Auto-Implemented Properties (C# Programming Guide)
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that 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:
// This class is mutable. Its data can be modified from // outside the class. class Customer { // Auto-Impl Properties for trivial get and set public double TotalPurchases { get; set; } public string Name { get; set; } public int CustomerID { get; set; } // Constructor public Customer(double purchases, string name, int ID) { TotalPurchases = purchases; Name = name; CustomerID = ID; } // Methods public string GetContactInfo() {return "ContactInfo";} public string GetTransactionHistory() {return "History";} // .. Additional methods, events, etc. } class Program { static void Main() { // Intialize a new object. Customer cust1 = new Customer ( 4987.63, "Northwind",90108 ); //Modify a property cust1.TotalPurchases += 499.99; } }
The class that is shown in the previous example is mutable. Client code can change the values in objects after they are created. In complex classes that contain significant behavior (methods) as well as data, it is often necessary to have public properties. However, for small classes or structs that just encapsulate a set of values (data) and have little or no behaviors, it is recommended to make the objects immutable by declaring the set accessor as private. For more information, see How to: Implement a Lightweight Class with Auto-Implemented Properties (C# Programming Guide).
Attributes are permitted on auto-implemented properties but obviously not on the backing fields since those are not accessible from your source code. If you must use an attribute on the backing field of a property, just create a regular property.
- 2/19/2012
- Sylvain Rodrigue
"How do you give a C# Auto-Property a default value?"
http://stackoverflow.com/questions/40730/how-do-you-give-a-c-auto-property-a-default-value
- 5/15/2011
- gerry lowry
But there is very subtle difference between the above two implementations. Properties are methods not fields so what they return is not same field but the copy of the original data (copy in case of value types and reference in case of reference types). So if you copy the reference types return value in a variable then you can change the value of field by manipulating your variable's content but still can't change its reference which is quite possible if you don't use properties.
- 3/1/2011
- Manoj Garg
- 3/1/2011
- Manoj Garg
Does not allow the parent class to set the property:
public string UserName { get; }Allows parent class to set property:
public string UserName { get; private set; }
- 10/28/2010
- Micah Burnett
- 10/28/2010
- Micah Burnett