readonly与常数有何不同。 [英] How the readonly is differ from constant.

查看:66
本文介绍了readonly与常数有何不同。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



你能告诉我,Readonly与C#中的Constant有什么不同。



谢谢,

Hi everyone,

Could you tell me that, How the Readonly is differ from Constant in C#.

Thank you,

推荐答案

包含必须在声明时初始化,readonly可以在构造函数上初始化(因此取决于构造函数具有不同的值)用过的)。 const是一个编译时常量,而readonly允许在运行时计算一个值并在构造函数或字段初始化程序中设置。



Contant must be initialized at declaration time, readonly can be initialized on the constructor (and thus have a different value depending on the constructor used). A const is a compile-time constant whereas readonly allows a value to be calculated at run-time and set in the constructor or field initializer.

区别在于静态只读字段的值设置为运行时,因此可以由包含类修改,而const字段的值设置为编译时常量。



在静态只读的情况下,包含类只允许修改它:

1.在变量声明中(通过变量初始值设定项)

2.在静态构造函数中(实例构造函数,如果它是不是静止的)

静态只读。



也允许实例只读字段。

The difference is that the value of a static readonly field is set at run time, and can thus be modified by the containing class, whereas the value of a const field is set to a compile time constant.

In the static readonly case, the containing class is allowed to modify it only:
1. in the variable declaration (through a variable initializer)
2. in the static constructor (instance constructors, if it's not static)

static readonly is typically used if the type of the field is not allowed in a const declaration, or when the value is not known at compile time.

Instance readonly fields are also allowed.





- Amy


Readonly:Readonly意味着一旦你分配了一个值您无法更改该值,而且可以更灵活地在声明后分配值。它保持不变。作为创建类的对象的副本。



常量:常量意味着一旦您将变量声明为常量,您必须赋值,即您需要在声明期间赋值。它在一个类的生命周期中保留一个副本。



更多信息请参阅 这个 [ ^ ] link
Readonly: Readonly means once you assigned a value you can not change that value and more over it has flexibility to assign value once after declaration . It maintains same no. of copies as objects created of the class.

Constant: Constant means once you declared a variable as constant you have to assign value i.e you need to assign value during declaration. It maintains a single copy in the life cycle of a class.

For more See This[^] link


两个不同点:

1)A 常量 value是一个固定项,在编译时绝对是已知的,并且具有隐式 static 声明:vlaue是相同的对于每个类的每个实例,它都可以通过类名访问,而不需要实例。

2)一个 readonly 值在编译时不是固定的,但它只能在类构造函数中指定。这允许将参数值分配给 readonly 变量,但永远不会再次更改。 readonly 值不必是静态的,可以是基于实例的,因此对于每个类的实例都是不同的。
Two differences:
1) A constant value is a fixed item which is known absolutely at compile time, and has an implicit static declaration: the vlaue is the same for each any every instance of the class, and it can be accessed via the class name without an instance being required.
2) A readonly value is not fixed at compile time, but it can only be assigned in the class constructor. This allows for parameter values to be assigned to a readonly variable, but never be changed again. readonly values do not have to be static and can be instance based, and thus different for each instance of a class.


这篇关于readonly与常数有何不同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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