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?

del.icio.us Tags: ,

  

Technorati Tags: ,

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • 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 […]
  • 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 […]
  • Intellisense Everywhere in VB 9 (and a small bug)Intellisense Everywhere in VB 9 (and a small bug) One of the features the CSharp guys have had for quite a while is Intellisense everywhere.  Want to create a private variable?  Just start typing private and hit the tab key as soon as the […]
  • Anonymous TypesAnonymous Types So, let's get to work looking at anonymous types in .NET. As you know, last week we looked at object initialization.  Object initialization allows us to instantiate an object and […]
  • The Ternary Operator in VB.NETThe Ternary Operator in VB.NET I think this may be the last operator that I really miss in VB from my curly brace language experience.  Although, I have to admit, I wouldn't have missed it all that much if they never […]

About Dave Bush

Dave Bush is a .NET programmer and Certified ScrumMaster who is passionate about managing risk as it relates to developing software. When he is not writing or speaking about topics related to Application Lifecycle Risk Management (ALRM), he is an example to his peers as he develops web sites in the ASP.NET environment using industry best practices.

One Pingback/Trackback

Awards & Certs


Links