有什么区别:readonly和const [英] What is the difference between: readonly and const

查看:68
本文介绍了有什么区别:readonly和const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于C#细节的一些内容,而且我只是读取了

语句readonly和const。我不明白,似乎是一样的,

有什么区别?这是它引用的文字:


const long size =((long)int.MaxValue + 1)/ 4;


常量值必须在编译时可计算


readonly DateTime日期;


只读字段 - 必须在其声明中或在

构造函数 - 值在编译时无需计算 - 消耗内存

位置(如字段)


(或它应该是后者,const是在堆栈上创建的,只读取堆上的
??

解决方案

嗨My4thPersonality,


const需要在编译时设置,而readonly可以在构造函数中动态生成

,但是你只能设置一次。 br />

readonly int readonlyInt;

const int constint = 0;


public MyClass(int n)

{

readonlyInt = n;

}

-

快乐编码!

M orten Wennevik [C#MVP]


My4thPersonality< so ******* @ out.there>写道:

我正在阅读有关C#细节的一些内容,并且我只读了一下
语句readonly和const。我不明白,似乎是一样的,
有什么区别?这是它引用的文本:

const long size =((long)int.MaxValue + 1)/ 4;

常量值必须在编译时可计算时间

readonly DateTime日期;

只读字段 - 必须在其声明或
构造函数中初始化 - 值不需要在编译时可计算 - 消耗一个内存
位置(就像一个字段)

(或者应该是后者,在堆栈上创建const并在堆上只读取它?)



Const直接编译成代码 - 如果你写的

const long Size = 100; //或者其他什么


后来


int x =尺寸;


它编译了声明/赋值x


int x = 100;


而不是作为Size成员的运行时引用。 />

这与堆栈/堆没有任何关系。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet

如果回复小组,请不要给我发邮件




Morten Wennevik <莫************ @ hotmail.com> schreef

const需要在编译时设置,而readonly可以在构造函数中动态生成,但是你只能设置一次。




感谢您的快速回答!


I am reading something about the details of C#, and I came acros the
statements readonly and const. I do not understand, it seems to be the same,
what''s the difference? Here is the text were it refers to:

const long size = ((long)int.MaxValue + 1) / 4;

Constant-Value must be computable at compile time

readonly DateTime date;

Read Only Field-Must be initialized in their declaration or in a
constructor-Value needs not be computable at compile time-Consumes a memory
location (like a field)

(Or it should be the latter, that const is created on the stack and readonly
on the heap??)

解决方案

Hi My4thPersonality,

const needs to be set at compile time while readonly can be generated
dynamically in a constructor, but you can set it only once.

readonly int readonlyInt;
const int constint = 0;

public MyClass(int n)
{
readonlyInt = n;
}
--
Happy Coding!
Morten Wennevik [C# MVP]


My4thPersonality <so*******@out.there> wrote:

I am reading something about the details of C#, and I came acros the
statements readonly and const. I do not understand, it seems to be the same,
what''s the difference? Here is the text were it refers to:

const long size = ((long)int.MaxValue + 1) / 4;

Constant-Value must be computable at compile time

readonly DateTime date;

Read Only Field-Must be initialized in their declaration or in a
constructor-Value needs not be computable at compile time-Consumes a memory
location (like a field)

(Or it should be the latter, that const is created on the stack and readonly
on the heap??)



Const is compiled directly into the code - if you write

const long Size = 100; // or whatever

then later

int x = Size;

it compiles the declaration/assignment of x as

int x = 100;

rather than as a run-time reference to the Size member.

It''s not really got anything to do with stack/heap.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



"Morten Wennevik" <Mo************@hotmail.com> schreef

const needs to be set at compile time while readonly can be generated
dynamically in a constructor, but you can set it only once.



Thanks for the quick answer!


这篇关于有什么区别:readonly和const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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