常数与只读字段 [英] constants vs. readonly-fields
问题描述
大家好,
如果我使用readonly字段而不是
常量,会有性能差异吗?
例如:
const int n = 100;
vs.
静态readonly int = 100;
或
const string s =" text";
vs.
static readonly string s =" text" ;;
Christof
Hi all,
Can there be a performance difference, if i use readonly fields instead of
constants?
e.g.:
const int n = 100;
vs.
static readonly int = 100;
or
const string s = "text";
vs.
static readonly string s = "text";
Christof
推荐答案
" Christof Nordiek" < cn@nospam.de>在消息中写道
news:uc ************** @ TK2MSFTNGP10.phx.gbl ...
"Christof Nordiek" <cn@nospam.de> wrote in message
news:uc**************@TK2MSFTNGP10.phx.gbl...
大家好,
如果我使用只读字段而不是
常量,会有性能差异吗?
例如:
const int n = 100;
vs.
static readonly int = 100;
const string s =" text";
vs.
static readonly string s =" ; text" ;;
Hi all,
Can there be a performance difference, if i use readonly fields instead of
constants?
e.g.:
const int n = 100;
vs.
static readonly int = 100;
or
const string s = "text";
vs.
static readonly string s = "text";
我没有亲自描述它,但我建议*如果*有任何
的区别,const会更快,因为它是初始化的编译时间,
而静态只读字段是运行时初始化的(因为它可以是静态分配的
)建设者。)。
-
问候,
Tim Haughton
Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
" Tim Haughton" < TI ********* @ gmail.com> schrieb im Newsbeitrag
新闻:Dk ******************* @ fe08.news.easynews.com。 ..
"Tim Haughton" <ti*********@gmail.com> schrieb im Newsbeitrag
news:Dk*******************@fe08.news.easynews.com. ..
" Christof Nordiek" < cn@nospam.de>在消息中写道
新闻:uc ************** @ TK2MSFTNGP10.phx.gbl ...
"Christof Nordiek" <cn@nospam.de> wrote in message
news:uc**************@TK2MSFTNGP10.phx.gbl...
大家好,
常量,那么会有性能差异吗?
例如:
const int n = 100;
vs.
static readonly int = 100;
const string s =" text";
vs.
static readonly string s =" ; text" ;;
Hi all,
Can there be a performance difference, if i use readonly fields instead
of
constants?
e.g.:
const int n = 100;
vs.
static readonly int = 100;
or
const string s = "text";
vs.
static readonly string s = "text";
我没有亲自描述它,但我建议*如果*有任何区别,那将是const因为它是初始化的编译时间,所以更快,
静态只读字段是运行时初始化的(因为它可以由静态构造函数分配。)。
-
Tim Haughton
Agitek
http://agitek.co.uk
http:// blogitek.com/timh aughton
感谢您的回答。
您谈论的是初始化。
但是如何访问那些领域/常数。
Christof
Thanks for answering.
Your talking about initialization.
But what about accessing those fields/constants.
Christof
>谢谢你回答。
你在谈论初始化。
但是如何访问这些字段/常量。
Your talking about initialization.
But what about accessing those fields/constants.
我可以'' t看到存在访问时间任何不一致的原因。一旦
他们被创造出来,他们只是无法触及的成员。
-
问候,
Tim Haughton
Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
这篇关于常数与只读字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!