Wat's Property?

Wat's Property?

PROPERTY:-Properties provide the opportunity to protect a field in a class by reading and writing to it through the property
it is a special method that can return a current object?s state or set it. Simple syntax of properties can see in the following example:

public int Old 
{
   get {return m_old;}
   set {m_old = value;}
 } 
public string Name 
{
   get {return m_name;} 



Example:-


       public class prop
{
private int age; 
public int humanAge
{
get //get accessor method
{
return age;
}
set //set accessor method
{
age = value;
}
}
} 

So when in main program(C#) user wants to use these set and get method to set and get person age. They will use it as

Perop boyAge = new Perop();  //create instance of object 
boyAge. humanAge = 10;  // set the age of boy by using setter method of object
int bAge = boyAge. humanAge;// get the age of boy by 
// using getter method of object 
Console.WriteLine(" Boy age is {0}"+ bAge ); 

Class Perop is property holder class. In which humanAge is property implementation.



value : In set method value variable is internal c# variable. User no needs to define it explicitly. 


By:kamal
Date:2012-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher