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


Other Related Items:

Woodturning with Ray Allen - DVD (All Regions Worldwide)After 30 years in the construction industry, Ray Allen (1930-2000) retired in 1982. In 1986, Allen found his passion when he began turning simple piec... Read More >
Thomas Kinkade How To Guide Book - Creating and Coloring Rubber Stamp Masterpieces - Full ColorThomas Kinkade How To Guide Book - Creating and Coloring Rubber Stamp Masterpieces - Full ColorThese techniques include a mixture of basic, intermediate & advanced, so there's something for everyone. The Thomas Kinkade How To Guide contains the ... Read More >

Related Post

One Response to “Simple Properties in C# 3.5”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor