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);
Class PersonAge is a property holder class. In which humanAge is property implementation. I will try to explain each line and how it works.
valueIn the set method value variable is the internal c# variable. User no need to define it explicitly.
boyAge.HumanAge = 10;
calls set method and sets the value for variable myAge as 10.
bAge = boyAge.HumanAge;
( last one in the program) calls get method and get value 10.
Now you may notice that the setter doesn't take any arguments. The value being passed is automatically placed in a variable named value that is accessible inside the setter method.
Someone may be wondering why they are not passing any data for set methods and how it works internally.
I want also to show how it internally works. I used my favorite ILDASM.exe (Intermediate Language Disassembler). (Read my article to know more about ILDAM).
Run ILDASM.exe XXXX.exe on command prompt.
The user will see the output below.
Two classes, one main and another property class, are displayed (by symbol;).
Click on the first-class user will see some two methods (by symbol ).
get_HumanAge: int32
set_HumanAge: void(int32)
You can see the name of the accessor methods because the compiler prefixes the property name with get_ (for a getter method) or set_ (for a setter method).
boyAge.HumanAge = 10;
It is basically.
boyAge.SetHumanAge(10);
if you will click on set_ and get_ methods in ILDASM output, you will get output as .method public hidebysig specialname instance int32.
get_humanAge() il managed
{
// Code size: 11 (0xb)
.maxstack 1
.locals (int32 V_0)
IL_0000: ldarg.0
IL_0001: ldfld int32 PersonAge::age
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret
} // end of method PersonAge::get_humanAge
set_humanAge(int32 'value') il managed
{
// Code size: 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld int32 PersonAge::age
IL_0007: ret
} // end of method PersonAge::set_humanAge
I am not going to explain the bit by bit of above code.
Note. Never try to use the get_and set_method of ILDASM output in your code. The compiler will generate an error.
If still, you have some doubts about properties in your mind, please mail me.
I do not write only because I want to write. I always try to present topics in the easiest way so that beginners in C# can understand.