Thursday, June 30, 2011

C# Shortcuts (Automatic Properties)


C # 3.0 introduces automatic properties. A property is usually (but not have to) to a private variable that is exposed to the outside world through getters and setters. The following is a common example of this



public class Employee
{
 private string _fName;
 public string FName
 {
    get { return _fName; }
    set { _fName = value; }
 }
}
Now see the magic...
public class Employee
{
 public string FName { get; set; }
}
C # compiler automatically creates a variable background and the right to get and set properties. Why is it useful? After all, you could have just done a string variable instead of a public class.

When you define as a property allows you to add validation logic in the current class at a later stage. The signature in the memory of the class will not change which means that any external library compiled code need not be recompiled

No comments:

Post a Comment