Home » c# » Simple Properties in C# 3.5

Simple Properties in C# 3.5

It’s such a little thing.  But, how much of our CSharp code looks something like this:

    private string _propertyName;     

    public string PropertyName 
        get { return _propertyName; } 
        set { _propertyName = value; } 

When I teach other programmers how to use CSharp (or VB) I always stress the importance of using properties instead of public member variables.  You never know when you’ll want your set to do some sort of validation and just about all of the databinding stuff requires us to use properties instead of member variables.  But, that’s a lot of code to write when all you want to do is wrap a member variable.

Well, in CSharp 3.5, life just got a lot sweeter.  That code above just got replace with this:

public string PropertyName { get; set; }

You can still use the code above if you want to.  But, why write all that code, even if you write it using a code snippet, when you can just write that one line?


Other post in c#

Related Post

  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]
  • CSharp adds the var keyword!CSharp adds the var keyword! There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files.  It will not significantly reduce the […]
  • Computed Columns Using LINQ to SQL ClassesComputed Columns Using LINQ to SQL Classes Last week we looked at the extension points Microsoft has wired into the LINQ to SQL classes and how they can be used to achieve some of the capabilities of the Business Logic Layer (BLL) […]
  • The VB.NET Ternary OperatorThe VB.NET Ternary Operator I think the VB.NET Ternary Operator may be the last operator that I really miss in VB.NET from my curly brace language experience.  Although, I have to admit, I wouldn't have missed it all […]
  • Assign Multiple enum Values To One VariableAssign Multiple enum Values To One Variable I saw this question and immediately thought, "You can't!  An Enum is an Integer that has been restricted to the values it can accept." And I was basically right.  But, […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback