什么是标志着场C#作为`readonly`好处? [英] What are the benefits to marking a field as `readonly` in C#?

查看:494
本文介绍了什么是标志着场C#作为`readonly`好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是具有只读声明为成员变量的好处是什么?难道仅仅是防止有人类的生命周期中更改或是否有任何编译器的速度提升,由于此关键字

What are the benefits of having a member variable declared as read only? Is it just protecting against someone changing during the lifecycle of the class or are there any compiler speed improvements due to this keyword

推荐答案

只读关键字用来声明一个成员变量不变,但可以计算的价值在运行时。这不同于与常量修饰符声明一个常量,它必须将其值设置在编译的时候。使用只读您可以设置字段的值或者在声明中,还是在对象的构造函数,该领域是其成员。

The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of.

也可以使用它,如果你不想重新编译引用该常数(因为它得到在编译时更换)外部DLL。

Also use it if you don't want to have to recompile external DLLs that reference the constant (since it gets replaced at compile time).

这篇关于什么是标志着场C#作为`readonly`好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆