Professional Documents
Culture Documents
Properties
Properties
Properties
Shailendra Chauhan
A Property acts as a wrapper around a field. It is used to assign and read the
value from a field by using set and get accessors. The code block for the get
accessor is executed when the property is read and the code block for the set
accessor is executed when the property is assigned a new value. A property can
be created for a public, private, protected and internal field.
Unlike fields, properties do not denote storage locations and you cannot pass a
property as a ref or out parameter.
class Example
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main()
{
Example obj= new Example();
obj.Name = "Dot Net Tricks"; // called set { }
Console.WriteLine(obj.Name); // called get { }
}
}
When to use
1. Need to validate data before assigning it to a field.
2. Need to do intermediate computation on data before assigning or retrieving it to
a field.
3. Need to log all access for a field.
4. Need to protect a field by reading and writing .
Static property
You can also declare a property static. To make a static property you must ensure
that the backing store field is also static. Typically, a static property is used to
make a singleton class.
public class Singleton
{
private static Singleton instance = new Singleton();
private Singleton() { }
Abstract property
An abstract property declaration does not provide an implementation of the
property accessors. It leaves the accessors implementation to derived classes.
Abstract properties are declared with in a abstract class or interface.
public abstract class Person
{
public abstract string Name{ get; set;}