C# to il 9 Properties and Indexers(属性和索引器)

A field is simply a memory location, whereas, a property is a collection of methods. A
property is represented by a value, in the same way as a field. Properties can be considered
as smart fields.
It is not compulsory to store the value of a property in a field, but this is the accepted
practice. The CLR supports the syntax of properties, but these properties do not exist at
runtime.

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

We have created a property called ff in the class aa. This property is written as a directive
called .property in the IL file, with the modifier instance, as it is a non-static property, and
with the return type int32.
There is an accessor called get, whose equivalent directive in IL is also called as .get. This
get is represented by the function get_ff, that simply returns a value with the data type of
the property.
In this case, the br instruction is superfluous(多余的). The local variable V_0 is used to store the
return value that is to be placed on the stack.
The statement int gg = a.ff + 9; gets executed in a unique way as follows:

The this pointer is placed on the stack.
Then, the expression a.ff get replaced by a call to a function get_ff from the class aa.
Thereafter, the return value is placed on the stack.
The number 9 is placed on the stack, followed by the add instruction.
The property gets converted into a function beginning with get.
On executing the il assembler 'ilasm' on a.il, you will see the following output 

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

A property with a set, obtains a function called set_ff, having a parameter called value. We
also have a directive called .set.
Ldarg.1 is used to place the first parameter of a function on the stack. The call to a
property a.ff = 19 gets converted into the function call. Thus, a property actually consists
of two functions, get and set. They get called, depending on whether we want to obtain the
value of a property or change it, respectively.

C# to il 9 Properties and Indexers(属性和索引器)

Error checks in IL are sparse(稀少的). We have a property called ff, which does not have either a
get or a set directive. The C# compiler screams at this omission, but the IL assembler turns
a blind eye to this.
Hopefully, the next version of IL should have more reasonable error checks. Having said
this, henceforth, we will not comment on the lack of error checks. It will be useful to

remember in the IL world, you are on your own. The excess freedom given by the IL
assembler also means that you have to assume greater responsibility as a programmer. 

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

A indexer is a property. It has no equivalent directive in IL. An indexer is simply a property
with an extra parameter, and no other complications. When we initialize a[1]using the
statement a[1] = 17, we are actually placing three parameters on the stack:
The this pointer
The array index 1
The value 17.

Then, we call set_Item, as it is an indexer and not a property. The two parameters to the
function are i and value. If you remember, the indexer variable has been named i.
The function get_Item gets called with the single parameter i and returns a value. The first
parameter to the WriteLine function is a string and the rest of the parameters are objects.
We need to convert our int value types into objects. Thus we need to box them.
Using the function set_Item, we are displaying the index and the value.
Using the function get_Item, we are displaying only the value.
Using the last WriteLine function, we are displaying the value of a[1], which is 23.
Thus, indexers are an alias for a property with an extra parameter.
The properties directive is used only by compilers and other tools, to understand as to
what methods are being associated with the property. If you are not convinced, you can
delete the property directive from the above programs and run them. There will be no
change at all in the way they execute. 

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

The above example demonstrates the use of virtual properties. The concept of a property is
simply an illusion. As mentioned earlier, properties are converted into a series of functions.
Thus what applies to virtual functions also applies to virtual properties. We cannot use the
modifier virtual in the properties directive.
The rationale behind using a property over a field is:
If the value of a field changes, no code gets called. The class is thus, unaware of the
change. In the case of a property, a method gets called. This method can contain a large
amount of code. This code can do anything.
Also, we can be very sure that the user does not change the value of the property beyond
certain acceptable limits. A method call can be optimised, and hence, a property does not
carry any significant overhead as compared to a direct access to a field. The only
disadvantage is that the properties cannot be made global.

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

A property normally has a field that stores the value associated with a property. Since the
property directive is used, for documentation purposes, it would not be a bad idea to have
a directive called backing, which can be used to state the name of this field. We are not
forced to do so. The assembler only checks to make sure that the filed is present. It is not
used in any way. It must have the same data type as the property. Using the attribute
specialname, we can inform(通知) the compiler to give it special treatment.

C# to il 9 Properties and Indexers(属性和索引器)

C# to il 9 Properties and Indexers(属性和索引器)

We finally have one last directive called .other, that specifies the other functions that are
associated with the property. In this case, the assembler does not check for the existence of
the function, and thus, we have not included it.
To summarise, the properties directive is implemented as a series of method calls. The
same is true for indexers also.