如何在内存中分配C#const成员? [英] How are C# const members allocated in memory?
问题描述
问题的标题不言自明.我想知道声明为 const
的成员是否是该类的所有实例的单例,还是每个实例都有其自己的副本.
The title of the question is self explanatory. I wonder if a member that is declared const
is singleton for all instances of the class or each instance has it's own copy.
我已经阅读了一些有关 const
的问题,但大多数问题都涉及方法内部的 const
变量.
I've read some questions about const
but most of them refer to const
variables inside a method.
推荐答案
常量通常可以在编译时进行评估,编译器很可能会将其替换为评估值.
Constants are usually something that can be evaluated compile time and compiler is likely to replace it with the evaluated value.
也 Const
表示 static
(请参见下面的链接)
Also Const
means static
(see link below)
常量表达式是可以在以下位置完全求值的表达式编译时间.因此,常数的唯一可能值引用类型为字符串和null.
A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and null.
来源:
这篇关于如何在内存中分配C#const成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!