A yet another topic and a great article on C#(ASP.net) to share,

The Properties in C#:

The usual way of haing fields made them unsafe to be accessed (read and write) by any external class that had an access to them. We can either make these variables unsafe through a access modifier of “Private”, but then they can not be used in the other classes.

To have a safer access to the member variables, where we can set the right to read or write of any field in C#, we have got an extraordinary feature added up as “Properties”

csharp Properties Example

Sample Code:

using System;

namespace SampleProgram

{

public class Employee

{

private int emp_id = -1; //field with private access

//Property for the field emp_id as ID

public int ID

{

get { return emp_id; }

set { emp_id = value; }

}

private string emp_name = string.Empty;//field with private access with in this class

//Property for the field emp_name as NAME

public string NAME

{

get { return emp_name; }

set { emp_name = value; }

}

}

//Lets define the other class to initiate the Main method

public class EmployeeManager

{

public static void Main(String[] args)

{

Employee emp = new Employee();

emp.ID = 1011; //initializing id with the property

emp.NAME = “Mahaveer”; //initializing name with the property

Console.WriteLine(“Employee Id:{0}, Employee Name:{1}”, emp.ID,emp.NAME);

Console.ReadKey();

}

}

}

Advantage of using Properties You might ask a question Isn’t it better to make a field public than providing its property with both set { } and get { } block? After all the property will allow the user to both read and modify the field so why not use public field instead?

Not always! Properties are not just to provide access to the fields; rather, they are supposed to provide controlled access to the fields of our class. As the state of the class depends upon the values of its fields, using properties we can assure that no invalid (or unacceptable) value is assigned to the fields.

Here we go with one more big big advantage of having the Properties are to have a control over the freedom to allow either write or read alone right to the class using this property with the object instantiated,

Lets see to an example for Read Only Properties:

public class Employee

{

private int emp_id = -1; //field with private access

//Property for the field emp_id as ID

public int ID

{

get { return emp_id; }

}

private string emp_name = string.Empty;//field with private access with in this

class

//Property for the field emp_name as NAME

public string NAME

{

get { return emp_name; }

}

//Constructor for initializing the fields

public Employee(int employeeId, string employeeName)

{

emp_id = employeeId;

emp_name = employeeName;

}

}

//Lets define the other class to initiate the Main method

public class EmployeeManager

{

public static void Main(String[] args)

{

Employee emp = new Employee(1011, “Mahaveer”);

Console.WriteLine(“Employee Id:{0}, Employee Name:{1}”, emp.ID, emp.NAME);

Console.ReadKey();

}

}

Here in the above example as you can find the fields are not given a set in the property, that makes it a read only property, hence these read only Properties can not initialized directly. Therefore we have them initialized in the “Custom Constructor” Employee(int employeeId,string employeeName).

As I said above, we can have both read opnly and write only Properties, I suppose you guys might have even got an idea of how to get this, just have a look at the below example that can make you understand it much easyly,

Write Only Properties example:

public class Employee

{

private int emp_id = -1; //field with private access

//Property for the field emp_id as ID

public int ID

{

set { emp_id = value; }

}

private string emp_name = string.Empty;//field with private access with in this class

//Property for the field emp_name as NAME

public string NAME

{

set { emp_name = value; }

}

//Method to display the Employee fields

public void DisplayEmployee()

{

Console.WriteLine(“Emplyee ID:{0}, Employee Nmae: {1}”, emp_id, emp_name);

}

}

//Lets define the other class to initiate the Main method

public class EmployeeManager

{

public static void Main(String[] args)

{

Employee emp = new Employee();

emp.ID = 1011;

emp.NAME = “Mahaveer”;

emp.DisplayEmployee();//To display the field values

Console.ReadKey();

}

}

This again as given above, allows the programmer only to have a write performed on the property, and so we have a method defined for displaying the Property that are written and so we get the output that displays the exact value that taken up by the Write only property.

Hope the examples have given a clear idea of what are the advantage of having our Properties defined for the fields. For any more queries on this you can get back to me.Keep visiting my blog for more interesting topics on C# and ASP.net.

%d bloggers like this: