Tuesday, March 16, 2010

What is Properties?

The easiest way to protect the fields of a class is to use properties. The properties are in fact methods like other member functions, but they facilitate the access of the private fields by the client. The client can use the properties in the same way it uses fields.
The C# properties are used like the following example:
private string item;
pubic string Item
{
    get
    {
        return item;
    }
    set
    {
        item = value;
    }
}
The field “item” is declared private, and the property “Item” is declared public. (This naming system is not mandatory but is a common convention.) A property contains the method get to read the field and the method set to assign a value to the field. The methods get and set are called accessors. They are classified as contextual keywords, which are words that have special meaning when used with properties or indexers.
The following example shows how to use the property to read or write a field name:
Item = “C# book”;    //using the set method
Console.Write(Item);    //using the get method
Note: - Although keywords are reserved words, contextual keywords are not. We cannot use a keyword as an identifier without prefixing it with the @ symbol, but contextual keywords can be used as identifiers.
Using Properties: -
Properties expose a convenient public way for reading and setting field values instead of dealing directly with the fields. One advantage of using properties as opposed to using fields directly is that we can validate the data before changing the value of the field. For example, if the field contains a specific data, we can check to see if the value to be assigned to the field is written a specific range.
Read – only Properties: -
Sometimes we need to protect the fields form any change by the user. For example, a field containing the company name should not be changed. In such cases, we omit the set accessor and keep the get accessor to read the field. This kind of property is called a read – only property, while the regular property is called a read – write property.
In the following example, both property kinds are used. The person’s name is a read –write property, while the company mane is a read – only property.
//Example 5-6.cs
//Read – only properties
using System;
public class Employee
{
    //Private fields:
    private string companyName =  “Microsoft”;
    private string employeeName;
    //EmployeeName – read – write property:
    public string EmployeeName
    {
        get { return employeeName; }
        set { employeeName = value; }
    }
    //CompanyName – read-only property:
    public string CompanyName
    {
        get { return companyName; }
    }
}
public class MainClass
{
    public static void Main()
    {
        Employee emp = new Employee();
        //Assign to the read – write property:
        emp.EmployeeName = “Hazem Abolrous”;
        //Read both the properties:
                  Console.WriteLine(“Company Name: {0}”, emp.CompanyName);
        Console.WriteLine(“Employee name: {0}”, emp.EmployeeName);
    }
}
Accessor Accessibility: -
In C# 2002, the two accessors, set and get, have the same accessibility as he property they act upon. In C# 2005, each accessor can have its own access level. This way, we can restrict the accessibility of the set accessor while keeping the get accessor public. In this case, the value of the value of the property can only be changed by the class designer.
The following example shows a property with public get and protected set.
public string EmployeeName
{
    get { return employeeName; }
    protected set { employeeName = value; }
}
There are, however, some restrictions when using this feature:
•    Access modifiers are allowed only on one of the accessors – usually it is the set accessor.
•    If we use one accessor only (as in the read – only property), we cannot use an access modifier on the accessor. Of course in this case, the accessor accessibility is the same as that of the property.
•    On interfaces, no access modifiers are allowed on accessors.
•    The accessibility of the accessor must be more restrictive than the accessibility of the property itself.

No comments:

Post a Comment