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.

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

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.

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.

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.

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.

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.

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.

C# to il 9 Properties and Indexers(属性和索引器)的更多相关文章

  1. C#中方法,属性与索引器

    C#中方法,属性与索引器: TODO: 1,关于系统中常常出现的通过某一字段,查询相应实体信息(可能是一条字段或一条数据和一组泛型集合) 解说篇:1,方法,2,属性3.索引器 1.方法(1.依据状态编 ...

  2. (转)c# 属性与索引器

    属性是一种成员,它提供灵活的机制来读取.写入或计算私有字段的值. 属性可用作公共数据成员,但它们实际上是称为“访问器”的特殊方法. 这使得可以轻松访问数据,还有助于提高方法的安全性和灵活性. 一个简单 ...

  3. set,get方法(属性,索引器)

    很多时候我们不可以把一些字段暴露出来允许别人调用和修改,为了隐藏这些字段又便于加限制的使用,在面向对象编程中一般采用写get set函数的办法,比如: //字段_age, "_"表 ...

  4. C#属性-索引器-里氏替换-多态-虚方法-抽象-接口-泛型-

    1.属性 //属性的2种写法 public class person { private string _name; public string Name { get { return _name; ...

  5. 通过getResourceAsStream 获得Properties文件属性和属性值

    1.Class.getResourceAsStream(String path) path:不以‘/’开头默认是从此类所在的包下取资源:以'/'开头则是从ClassPath根目录下获取 2.Class ...

  6. SpringBoot @Value读取properties文件的属性

    SpringBoot在application.properties文件中,可以自定义属性. 在properties文件中如下示: #自定义属性 mail.fromMail.addr=lgr@163.c ...

  7. gradle-wrapper.properties中各属性的含义

    gradle-wrapper.properties中各属性的含义 1. gradle-wrapper.properties 每一个用gradle编译的工程,都会有一个gradle\wrapper目录. ...

  8. springboot成神之——application.properties所有可用属性

    application.properties所有可用属性 # =================================================================== # ...

  9. SpringBoot 获取properties配置文件的属性

    自定义properties文件获取属性 使用 @ConfigurationProperties((prefix = "demo")) 和 @PropertySource(" ...

随机推荐

  1. 重载的方式写Python的post请求

    #encoding=utf-8#__author__="Lanyangyang" import unittestimport requestsimport json # This ...

  2. shiro简单学习的简单总结

    权限和我有很大渊源. 培训时候的最后一个项目是OA,权限那块却不知如何入手,最后以不是我写的那个模块应付面试. 最开始的是使用session装载用户登录信息,使用简单权限拦截器做到权限控制,利用资源文 ...

  3. 基于.NET Core 框架搭建WebApi项目

    一 什么是.NET Core? 随着2014年 Xamarin和微软发起.NET基金会,微软在2014年11月份开放.NET框架源代码.在.NET开源基金会的统一规划下诞生了.NET Core .也就 ...

  4. <Scala><For beginners>

    Scala Overview Scala is object-oriented Scala is a pure object-oriented language in the sense that e ...

  5. 关于orm 的基础3 day67

    day67 ORM 特殊的语法 一个简单的语法 --翻译成--> SQL语句 语法: 1. 操作数据库表 创建表.删除表.修改表 2. 操作数据库行 增.删.改.查 怎么连数据库: 需要手动创建 ...

  6. ubantu创建python虚拟环境

    安装虚拟环境的命令如下: sudo pip install virtualenv sudo pip install virtualenvwrapper 创建虚拟环境的命令如下: mkvirtualen ...

  7. Day10作业及默写

    1,继续整理函数相关知识点,写博客. 2,写函数,接收n个数字,求这些参数数字的和.(动态传参) def func(*number): sum=0 for num in number: sum+=nu ...

  8. sql 按字段指定值排序

    这个需要在排序语句中使用条件判断 例如:表[Table_temp]中列[col1]为字符,属性为varchar(10),排序时需要按照B.A.C的顺序显示,则可按照以下SQL语句: select * ...

  9. 三层交换机实现VLAN间通信

    实验要求:使用三层交换机,让同一vlan的主机能通信,不同vlan的主机也能通信 拓扑如下: 涉及内容: 1.VTP的创建和配置 2.vlan的创建和划分 3.三层交换机的配置 4.端口的trunk模 ...

  10. 检查服务器主从状态的脚本-check_server_state.sh

    分别检查服务器在Master/Slave状态下,各项服务是否正常,否则报警: 原来使用keepalived每隔1分钟调用,由于执行结果对keepalived的weight参数有影响,所以移动到外部,使 ...