Properties in C#

Hi all, in this post I will explore the concept of properties in C# this concept is same across different languages in .NET

Defn: A property is a member of a class that is used to write data into the data member and read data from the data member of a class.

A property can never store data, just used to transfer the data.

Need for properties

Consider a class
Class Employee
{
public String  EmpName;
public int  EmpID;
}

To use the data members of the class in another class we have to first create the object for the class and using the object we have to call the data members of the class as,

Employee obj1 = new Employee();
obj1.EmpName  =  CRL();
obj1.EmpID = Convert.ToInt32(CRL());
CWL(“Employee Name is s : ”obj1.EmpName);
CWL(“Employee ID  is  : ”obj1.EmpID);

In the above class the accessibility of the data members are public

Here in the above class the data members are public but if the data members have the accessibility as private then we cannot use obj1.EmpName or obj1.EmpID
Instead we can write the code as follows

Class Employee
{
string  empName;
int  empID;
public void SetEmpName(string temp)
{
EmpName = temp;
}
public string GetEmpName()
{
return EmpName;
}
public void SetEmpID(int temp)
{
EmpName = temp;
}
public int GetEmpID()
{
return EmpName;
}
}

Here we can observe that the properties are private and are accessible to outside the Employee class using the Get and Set methods.

Here the data fields are still private, only the Get and Set methods are public

In C# there is a built in mechanism to perform the above, and the mechanism is called properties

Class Employee
{
string  empName;
int  empID;
public string EmpName
{
set
{
EmpName = value;
}
get
{
return EmpName;
}
}
public int EmpID
{
set
{
EmpID  = value;
}
get
{
return EmpID;
}
}
}

Here eventhough the data members are private we are still able to transfer the data to the data members but instead of Get and Set method we are using properties EmpName and EmpID to access the data members of the Employee class

Employee obj1 = new Employee();
//Here automatically the set assessor is called
obj1.EmpName  =  CRL();
obj1.EmpID = Convert.ToInt32(CRL());
//Here automatically  the get assessors  is being called
CWL(“Employee Name is s : ”obj1.EmpName);
CWL(“Employee ID  is  : ”obj1.EmpID);

Explanation

In General, Outside the class we perform the following two operations on the data fields

1) Write the data into the data member
2) Read the data from the data member

There are two assessors/methods that are available to perform this write and read operation
1) set assessor
2) get assessor

The code block for the get accessor is executed when the property is read;
The code block for the set accessor is executed when the property is assigned a new value

SET ASSESSOR

set assessor is used to write the data into the data member of a class
set assessor resembles a method whose return type is void
set assessor contains a default and fixed variable/parameter called value the type of variable is the type of the property

Whenever we call the property for writing the data then set assessor is called automatically and the data that we transfer will be stored into the value variable by default

Syntax-
set
{
datafieldname = value;
}

Example-
set
{
empID = value;
}

GET ASSESSOR

get assessor is used to read the data from the data member of the class
Whenever we call the property for reading the data get assessor is called automatically and will return the data field data

Syntax-
get
{
return datafieldname;
}

Example-
get
{
return empID;
}

Types of properties

C#.NET supports three types of propertied

1) Read only property
2) Write only property
3) Read Write property

READ ONLY PROPERTY

A Read only property is used to read the data from the data field
We cannot write the data using read only property
This property contains only one assessor i.e. get assessor

Sytnax-
AccessModifier DataType  PropertyName
{
get
{
return datafieldname;
}
}

Example-
public  int  EmpID
{
get
{
return empID;
}
}

WRITE ONLY PROPERTY

A Write only property is used only to write the data into the data field
We cannot read the data using write only property
This property contains only one assessor i.e. set assessor

Sytnax-
AccessModifier DataType  PropertyName
{
set
{
datafieldname =value;
}
}

Example-
public  void EmpID
{
set
{
empID = value;
}
}

READ WRITE PROPERTY

A Read Write property is used to perform both read and write operation
This property contains both get and set assessor

Sytnax-
AccessModifier DataType  PropertyName
{
set
{
datafieldname =value;
}
get
{
return datafieldname;
}
}

Example-
public  void EmpID
{
set
{
empID = value;
}
get
{
return empID;
}
}

Advantages of properties

1)Properties provide a layer of abstraction
(we can have different property name from the data field name)
2)Properties provide security to the data fields
(we can make a data meber read only thus providing the security)
3)Properites can be used to validate data before reading and writing operations.

Example -
set
if ((value > 0) && (value< 13))
{
month = value;
}

Here if the data is stored only if the value is between 1 to 12

get
{
return name == null ? "NA" : name ;
}

Here if name has null then NA is returned.

-
Further Reading

http://msdn.microsoft.com/en-us/library/w86s7x04%28v=vs.100%29.aspx