Properties in C# : A new cover on old book
I do not write articles for the sake of writing them. I always try topresent topics in an easy way so that beginner in C# can understandthem.
–Vivek
Properties are accessormethods whose job it is to retrieve and set the values of fields. Itprovides a way to protect a field in a class by reading and writing andalso support object-oriented concept of encapsulation. In otherlanguages, users can use this feature using getter and setter methods.When users wants to wrap a value in an attribute of an object or getthe value from attributes of an object and when want to use attributevalue, it is always useful to use setter method to wrap value inattribute and getter method to retrieve value from attribute of anobject. Properties provide a level of abstraction to user.
Most object-oriented programmers use getter and setter methods. As in Java language users use accessor method as below.
A Java Program
public class PersonAge
{
private int age; // private local variable
public void getAge() //get accessor method
{
return age;
}
public void setAge(int age) //set accessor method
{
myAge = age;
}
}
In the main program, the user wants to use these set and get methods to set and get person 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
The setter and getter method iare very useful in the object-orientedworld. In .NET framework in C# these setter and getter methods arepresented under the heading properties. Now in .NET framework by way ofpresentation of properties are very smoother and useful to theprogrammer.
Above code in C#
public class PersonAge{
private int age; // private local variable
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 methods to set and get a persons age. They will use it as:
PersonAge boyAge = new PersonAge(); //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 PersonAge is theproperty holder class, in which humanAge is property implementation. Iwill try to explain about each line of code.
value : In set method value variable is internal c# variable. User no needs to define it explicitly.
boyAge.humanAge = 10; calls set method and set the value for variable myAge as 10.
bAge = boyAge. humanAge( last one in program) calls get method and get value 10;
Now you may have noticed thatthe setter doesn't take any arguments. The value being passed isautomatically placed in a variable named value that is accessibleinside the setter method.Some one may wonder that when they are not passing any data for setmethods how it works internally.I also want to show how it internally works. I used my favoriteILDASM.exe (Intermediate Language Disassembler). (Read my article toknow more about ILDAM)
Run ILDASM.exe XXXX.exe on command prompt
User will see output as below.

Two classes one main and other property class are displayed (by symbol
)
Click on first class user will see some two methods (by symbol
).
get_humaneAge: int32 and set_humanAge:void(int32)
You can see the name of theaccessor methods because the compiler prefixes the property name withget_ (for a getter method) or set_ (for a setter method). So code
boyAge.humanAge = 10; isbasically boyAge.set_humanAge(10). if you will click on set_ and get_methods in ILDASM output you will get out put 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
And
.method public hidebysig specialname instance void
set_humanAge(int32 'value') il managed
{
// Code size 8 (0×8)
.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 bit by bit of above code.
WARNING: Never try to use get_ and set_ method of ILDASM output in your code. Compiler will generate error.
If still you have some doubts about properties in your mind please mail me.












No comments yet... Be the first to leave a reply!