如何在内存中分配C#const成员? [英] How are C# const members allocated in memory?

查看:69
本文介绍了如何在内存中分配C#const成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的标题不言自明.我想知道声明为 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变量内存

C#中const的内存分配

C#const字段会使用更少的内存吗?

这篇关于如何在内存中分配C#const成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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