Introduction:
In this article helps you to understand c# properties and its uses.
Main:
Definition:
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.
Advantages:
Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. For more information, see Accessor Accessibility.
The value keyword is used to define the value being assigned by the set indexer.
Properties that do not implement a set method are read only.
See the below simple ex,
class Person
{
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
set
{
name = value;
}
}
}
Person p1 = new Person();
p1.Name = "James"; // the set accessor is invoked here
System.Console.Write(p1.Name); // the get accessor is invoked here
class Person { private string name; // the name field public string Name // the Name property { get { return name; } set { name = value; } } } Person p1 = new Person(); p1.Name = "James"; // the set accessor is invoked here System.Console.Write(p1.Name); // the get accessor is invoked here |
The above examble clearly illustates,how to use c# properties,
See this another brief examble,
public class Employee
{
public static int NumberOfEmployees;
private static int counter;
private string name;
// A read-write instance property:
public string Name
{
get { return name; }
set { name = value; }
}
// A read-only static property:
public static int Counter
{
get { return counter; }
}
// A Constructor:
public Employee()
{
// Calculate the employee's number:
counter = ++counter + NumberOfEmployees;
}
}
class TestEmployee
{
static void Main()
{
Employee.NumberOfEmployees = 100;
Employee e1 = new Employee();
e1.Name = "James";
System.Console.WriteLine("Employee number: {0}", Employee.Counter);
System.Console.WriteLine("Employee name: {0}", e1.Name);
}
}
public class Employee { public static int NumberOfEmployees; private static int counter; private string name; // A read-write instance property: public string Name { get { return name; } set { name = value; } } // A read-only static property: public static int Counter { get { return counter; } } // A Constructor: public Employee() { // Calculate the employee's number: counter = ++counter + NumberOfEmployees; } } class TestEmployee { static void Main() { Employee.NumberOfEmployees = 100; Employee e1 = new Employee(); e1.Name = "James"; System.Console.WriteLine("Employee number: {0}", Employee.Counter); System.Console.WriteLine("Employee name: {0}", e1.Name); } } |
Conclusion:
Hope,this helps
Happy Coding.
References:
msdn.microsoft.com