in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, MOSS 2007, ASP.NET, LINQ, and Visual Studio 2012.

C# 3.0: Automatic Properties

In reading blogs today, I had overlooked this feature and I am not sure what to think about it so I thought I would put it out there and see what you all think. Take the following simple class with a property.

public class MyClass
{
    private string myString;
 
    public string MyString;
    {
         get {  return myString; }
         set { myString = value; }
     }
}

A new feature in C# 3.0, allows you to simplify the syntax to the following.

public class MyClass
{
    public string MyString;
    {
         get;
         set;
     }
}

Behind the scenes, this stll functions as a property and the C# compiler does in fact generate a hidden private variable. The compiler automatically generates the get/set logic for you. From accessing the class from another object this makes things great and you access these public properties just like you would normally do. However, there is no access to the "hidden" private variable. So from within the class you have to access the public property. This I am not sure if I like. What do you all think?

Want to know how it really works when it comes down to the IL? Read the following post.

C# 3.0 Automatic Properties Explained

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=364

2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems