Pages

Men

rh

10/19/2013

Properties in C#

Properties:-
  • Properties are member of a class, and used to write the data in the data field and read the data from the data field of the class.
  • A Property can never store the data, just used to transfer the data.
  • Properties are members that provide a flexible mechanism to read , write or compute the values of private fields.
  • Properties enables a class to expose a public way of getting and setting values, while hiding implementation or verification code.
  • To perform read and write operations, properties can contain two Accessors/Methods.
Accessors enables the data to be accessed easily while still providing safety and flexibility of methods.
  • Set Accessors
  • Get Accessors

Set Accessors:-
Set Accessors are used to write the data into data field.
They will contain a default and flexible variable named as Value.
Whenever we call the property to write the data, any data we supply will come and store the in value variable by default.

Syntax:-
Set
{
   Data filename = value;
}

Get Accessors:-
Get Accessors are used to read the data from the data field. Using this accessor we can't write the data.

Syntax:-
Get
{
   return Data filename;
}

Types of Properties:-
Three types of properties are available .
  • Read only Property
  • Write only Property
  • Read Write Property
Read only Property:-
  • They are used to read the data from the data field.
  • We can't write the data into data filed using this property.
  • This property will contain only one accessor i.e Set Accessor.
Syntax:-
Set
{
   Data filename = value;
}

Write Only Property:-
This is used to write the data into Data field of a class.
Using this property we can't read the data from the data field.
This property will contain only one accessor i.e Get Accessor.

Syntax:-
Get
{
   return Data filename;
}

Read Write Property:-
  • This is use
  • d to read the data from the data field and write the data into the data field.
  • This property will contain two accessors. They are Get and Set
Syntax:-
Set
{
   Data filename = value;
}
Get
{
   return Data filename;

Advantages of Properties:-
  • Property will  provide Abstraction to the data fields.
  • Properties will provide security to the data fileds.
  • Properties can be used to validate before going allowing a change.

Example for Read and Write the Property:-
Class clsEmployee
{
   Int32  intEmpId;
   String strEmpName ;

    public int32 EmployeeId
   {
       Set
        {
             intEmpId = value;
        }
        Get
        {
            return  intEmpId ;
         }
  }
  public String EmployeeName
   {
       Set
        {
             strEmpName = value;
        }
        Get
        {
            return  strEmpName ;
         }
  }

}

class clsMain
      static void  Main( String[] )
      clsEmployee obj = new clsEmployee();
      obj.EmployeeId = Convert.Toint32(Console.Readline());
      obj.EmployeeName = Console.ReadLine();
     Console.WriteLine("Employee Id is...." + obj.EmployeeId );
     Console.WriteLine("Employee Name is...." + obj.EmployeeName );
     Console.ReadLine();
}



Example  for Read Only and Write Only Properties:-

Class clsEmployee
{
   Int32  Num1,Num2, Re;
  public int32 Number1
  {
       set { Num1 = value;}
  }
 public int32 Number2
  {
       set { Num2 = value;}
  }
 public int32 Result
  {
       Get{return Res;}
  }

public void Add()
{
   Res = Num1 + Num2;
}

}
 class ClsMain
      static Void Main()
     {
           clsEmployee obj = new clsEmployee();
          obj.Number1= Convert.Toint32(Console.Readline());         
          obj.Number2= Convert.Toint32(Console.Readline());         
          obj.Add();
          Console.WriteLine("Sum of two numbers is..." + obj.Result);
          Console.ReadLine();
     }
}


No comments :

Post a Comment