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? Tags: ,


Technorati Tags: ,

Related Post

One Response to “Simple Properties in C# 3.5”