Automatic properties
In the past, if we wanted to define a class member as a property member, we had to define a private member variable first. For example, for the Product
class, we can define a property, ProductName
, as follows:
private string productName; public string ProductName { get { return productName; } set { productName = value; } }
This may be useful if we need to add some logic inside the get
or set
methods. But if we don't need to the above format gets tedious, especially if there are many members.
Now, with C# 3.0 and above, the previous property can be simplified into one statement:
public string ProductName { get; set; }
When Visual Studio compiles this statement it will automatically create a private member variable, productName,
and use the old style's get
or set
methods to define the property. This could save lots of typing.
Just as with the new type, var
, the automatic properties are only meaningful to the Visual Studio...