Behind the Scenes Look at C#: Properties continued - The Name Property
(Page 2 of 4 )

It's defined using the directive .property and marked as an instance member using the instance keyword, followed by the data type of the property and finally the name of the property. The get method is listed here using the .get directive. Note that the return data type is the same as the data type of the property. The set method is listed in the property using the .set directive. Note that it has only one parameter of type string. Now let's take a look at the methods MSIL code. Double click on the get_Name method.

Again, spending some time understanding the MSIL generated code crystallizes in your mind how the C# code gets executed. The get_Name method returns the value of the property (through the private field). The ldarg.0 instruction loads the first parameter, and as we said before, the get_Name method is a parameterless method.
What actually happens is that the MSIL code always refer to the this pointer as the first method's argument. So all the instance methods have the first argument (it's zero-based so it's number 0) refer to the this pointer. The ldfld instruction (stands for Load Field, as we have said before) loads the field of the object in hand. In other words, it loads the field of the object that we have a pointer to (the this pointer from the first step). As you can see it loads the field Worker::name.
The instruction stloc.0 stores the value of the name field into the first local variable, and the ldloc.0 instruction (stands for Load Location) loads the value back to the stack. The method returns with the value of the field. Let's take a look at the set_Name method.

The first issue I want to discuss is the Set_Name(string 'value'). As you know, 'value' is a keyword in C# so you can't just use it in your code as an identifier. The C# compiler always generates the set_xxx methods with one parameter named 'value,' and it has the same data type as the property. The get method returns the same data type as defined in the property too, so it's just one data type in both of the methods. In other words, there was no magic when you used an expression like this.name = value to assign the value of the value parameter to the private field name.
Okay, the ldarg.0 loads the this pointer on the stack, then the instruction ldarg.1 loads the argument number 2 (which is the 'value' parameter) on the stack. After that, the instruction stfld stores the value of the 'value' parameter to the field Worker::name, and then the method returns using the instruction ret. I think that now you have a better understanding of the generated MSIL code. Let's look at the Main method's MSIL code.

I think that now, understanding the Main method is not a big issue. The newobj instruction instantiates a new object (in our case an object of type worker). The stloc.0 stores the reference of the person object on the stack, then the ldloc.0 loads the reference. The ldstr "Michael" instruction loads the string Michael, then a call to the method set_Name(string) is put in place by using the instruction callvirt (call virtual method). Note that the constant value "Michael" is passed to the method (through the instructions) and that's why the value keyword in C# (which is the name of the paramter in MSIL) refers to the value itself (in our example "Michael").
Again, we load the location zero using the instruction ldloc.0, which contains the object reference person, and call the method get_Name(). Then we call the Console.WriteLine method and return to the caller using the instruction ret. Some of you may think that using a property to control the access to a field will slow down the application performance, but this is far from the truth. Actually the runtime JIT compiler knows how to optimize the code of the more method calls (the get and set methods which result in more lines of code to access the field), and believe it or not, it generates even faster code than the code that uses public fields.
I think many of you will want to go to the nearest bookshop and get a book about MSIL and the CLR. I advise you to master C# first, then get a book about MSIL. In fact, it's uncommon to write MSIL using the assembler, but there are some very scientific applications which need just that. Compiler designers who need to develop a compiler for their programming languages that target the .NET runtime will also find that they need to master MSIL code.
We have been discussing properties on instances, but we haven't discussed static properties, so let's do that.
Next: Static Properties >>
More C# Articles
More By Michael Youssef