Home » none » 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?

Related Post

  • 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 […]
  • 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 […]
  • 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 VB.NET Ternary Operator The 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 […]

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