Introduction
Properties are accessor methods whose job it is to retrieve and set the values of fields. It provides a way to protect a field in a class by reading and writing and also supports the object-oriented concept of encapsulation. In another language, users can use this feature using getter and setter methods. When a user wants to wrap a value in the attribute of an object or get the value from the attribute of an object and when wants to use attribute value. It is always useful to use the setter method to wrap the value in the attribute and the getter method to retrieve the value from the attribute of an object. Properties provide a level of abstraction to the user.
Most object-oriented programmers use getter and setter methods. As in Java language, the user used the accessor method as below.
Example
public class PersonAge
{
private int age; // Private instance variable
public int GetAge() // Get accessor method
{
return age;
}
public void SetAge(int newAge) // Set accessor method
{
age = newAge;
}
}
In the main program, the user wants to use these sets and get methods to set and get a person's age. They will use it as.
PersonAge boyAge = new PersonAge(); //create instance of object
boyAge.setAge(10); // set the age of boy by using setter method of object
Syetm.out.println("Boy age is "+boyAge.getAge()); // get the age of boy by using
// getter method of object
This setter-and-getter method was very useful in the object-oriented world.
In the .NET framework in C#, these setter and getter methods are presented under the heading properties. Now in the .NET framework way of presentation of properties is very smoother and useful to the programmer.
Example
public class PersonAge
{
private int age; // Private instance variable
public int HumanAge
{
get // Get accessor method
{
return age;
}
set // Set accessor method
{
age = value;
}
}
}
So when in the main program(C#) user wants to use these sets and get the method to set and get a person's age. They will use it as.
PersonAge boyAge = new PersonAge(); // Create an instance of the object
boyAge.HumanAge = 10; // Set the age of the boy using the property of the object
int bAge = boyAge.HumanAge; // Get the age of the boy using the property of the object
Console.WriteLine("Boy age is {0}", bAge);

).
Join the conversation! Your thoughts help the community grow.