哪些类型可以声明为const? [英] What types can be declared as const?
问题描述
在C#中,可以声明为 const
的类型是什么?
In C#, which are the types that can be declared as const
?
const int i = 0;
const double d = 0;
const decimal m = 0;
const referenceType = null;
是否有我可以参考的完整列表?
Is there a comprehensive list I can refer?
推荐答案
很好 MSDN 明确指出
常量表达式是可以在
编译时完全评估的表达式。因此,
引用类型的常量的唯一可能值为字符串和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#语言规范的10.4节。可以使用这些类型。
From section 10.4 of C# language specification. These are the types that can be used.
在常量声明中指定的类型必须是sbyte,byte,
short, ushort,int,uint,long,ulong,char,float,double,decimal,
bool,字符串,枚举类型或引用类型。每个
常量表达式都必须产生目标类型或
类型的值,可以通过隐式转换将其转换为目标类型
The type specified in a constant declaration must be sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. Each constant-expression must yield a value of the target type or of a type that can be converted to the target type by an implicit conversion
这篇关于哪些类型可以声明为const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!