const对静态只读的优势? [英] Advantage of const against static readonly?
本文介绍了const对静态只读的优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道使用const而不是静态只读的好处是什么。
静态只读是运行时常量,可以在初始化程序中设置一次
或静态ctor,而const遭受二进制不兼容
,因为它被硬化为二进制文件。
我不相信有性能优势使用const over
静态只读,因为JIT会考虑到这一点,或者我错了吗?
Iam wondering what the benefit of using const over static readonly is.
static readonly is a runtime constant and can be set once in the initializer
or the static ctor, whereas const is suffering from binary incompatibility
since it is hardbaked into the binary.
I do not believe there is a performance advantage with using const over
static readonly since the JIT will take account of that or am I wrong here?
推荐答案
一个值得注意的区别是,由于它们没有加载到内存中,因此它们不会消耗任何内存,因为它们不会被加载到内存中。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
One notable difference is that consts don''t consume any memory at
runtime since they aren''t loaded into memory.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
你不担心静态只读值的内存消耗
由于它的存在而只存在一次静态性质,你是:)
不是相反的情况:如果你的const是一个相当大的结构,它将在每个使用这个库的二进制文件中硬烘烤。
我怀疑JIT会生成const和静态readonly
完全相同的代码,因为我无法想象为什么我不会这样的原因
可以通过这种方式优化代码。
" Mattias Sj?gren" <毫安******************** @ mvps.org> schrieb im Newsbeitrag
新闻:%2 ****************** @ tk2msftngp13.phx.gbl ...
You aren''t worried about the memory consumption of a static readonly value
which exists exactly one time due to its static nature, are you :)
No the opposite is the case: If your const is a rather large struct it will
be hardbaked in each binary which uses this library.
I suspect that the JIT will generate both both const and static readonly
exactly the same code as I cannot imagine a reason why I would not be
possible to optimize the code this way.
"Mattias Sj?gren" <ma********************@mvps.org> schrieb im Newsbeitrag
news:%2******************@tk2msftngp13.phx.gbl...
一个值得注意的区别是,它们不会在运行时消耗任何内存,因为它们不会被加载到内存中。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请仅回复新闻组。
One notable difference is that consts don''t consume any memory at
runtime since they aren''t loaded into memory.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
const'的值必须在编译时进行评估,而readonly'的
值可以设置在运行时在类的构造函数中。这意味着
const只能是任何原始类型或字符串,
而readonly字段可以是任何类型。
每次实例化
类时,只读字段可以为其分配不同的值,每个对象的const字段具有相同的值
已创建。
/ Joakim
cody写道:
A const''s value must be valuated at compile time, whereas a readonly''s
value can be set at runtime in the constructor of the class. This means
that a const can only be of any of the primitive types or a string,
whereas a readonly field can be of any type.
A readonly field can have different values assigned to it every time the
class is instantiated, a const field has the same value for every object
created.
/Joakim
cody wrote:
我想知道使用const比静态readonly有什么好处是静态readonly是一个运行时常量,可以在初始化程序中设置一次
或静态ctor,而const则存在二进制不兼容性,因为它被硬化为二进制文件。
我不认为使用const over
static readonly会有性能优势,因为JIT会考虑到这一点,或者我在这里错了吗?
Iam wondering what the benefit of using const over static readonly is.
static readonly is a runtime constant and can be set once in the initializer
or the static ctor, whereas const is suffering from binary incompatibility
since it is hardbaked into the binary.
I do not believe there is a performance advantage with using const over
static readonly since the JIT will take account of that or am I wrong here?
这篇关于const对静态只读的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文