Thursday, January 3, 2008

C# : Auto-Implemented Properties

Auto-Implemented Properties :

Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field can only be accessed through the property’s get and set accessors.

The following example shows a simple class that has some auto-implemented properties:
class Emplyee
{
public int ID { get; private set; } // read-only

public string Name { get; private set; } // read-only

public string Destination { get; set; }
}
Auto-implemented properties must declare both a get and a set accessor. To create a readonly auto-implemented property, give it a private set accessor.

Attributes are not permitted on auto-implemented properties. If you must use an attribute on the backing field of a property, just create a regular property.

Share your thoughts with me !!!
- Rangoli