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