简体   繁体   English

哪些类型可以声明为const?

[英]What types can be declared as const?

In C#, which are the types that can be declared as const ? 在C#中,可以声明为const的类型有哪些?

const int i = 0;
const double d = 0;
const decimal m = 0;
const referenceType = null;

Is there a comprehensive list I can refer? 有没有我可以参考的综合清单?

Well MSDN clearly states that MSDN明确指出

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. 因此,引用类型的常量唯一可能的值为string和null。

From section 10.4 of C# language specification. 从C#语言规范的10.4节开始。 These are the types that can be used. 这些是可以使用的类型。

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. 常量声明中指定的类型必须是sbyte,byte,short,ushort,int,uint,long,ulong,char,float,double,decimal,bool,string,枚举类型或引用类型。 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 每个常量表达式必须产生目标类型的值或可以通过隐式转换转换为目标类型的类型的值

From MSDN: 从MSDN:

Constants are immutable values which are known at compile time and do not change for the life of the program. 常量是不可变的值,在编译时就知道这些值,并且在程序生命期内不会更改。 Constants are declared with the const modifier. 常量使用const修饰符声明。 Only the C# built-in types (excluding System.Object) may be declared as const . 只能将C#内置类型(不包括System.Object)声明为const For a list of the built-in types, see Built-In Types Table (C# Reference). 有关内置类型的列表,请参见内置类型表(C#参考)。 User-defined types, including classes, structs, and arrays, cannot be const. 用户定义的类型(包括类,结构和数组)不能为const。 Use the readonly modifier to create a class, struct, or array that is initialized one time at runtime (for example in a constructor) and thereafter cannot be changed. 使用readonly修饰符创建一个类,结构或数组,该类,结构或数组在运行时初始化一次(例如在构造函数中),此后无法更改。

C# does not support const methods, properties, or events. C#不支持const方法,属性或事件。

complete link : http://msdn.microsoft.com/en-us/library/ms173119.aspx 完整链接: http : //msdn.microsoft.com/zh-cn/library/ms173119.aspx

In the context of C#, a constant is a type of field or local variable whose value is set at compile time and can never be changed at run time. 在C#的上下文中,常量是一种字段或局部变量,其值在编译时设置,并且在运行时不能更改。 It is similar to a variable by having a name, a value, and a memory location. 通过具有名称,值和存储位置,它类似于变量。 However, it differs from the variable by its characteristic of getting initialized only once in the application. 但是,它与变量的区别在于它在应用程序中仅初始化一次的特征。 A constant is declared using the keyword "const". 使用关键字“ const”声明一个常量。

Constants (C# Programming Guide) 常量(C#编程指南)

Only the C# built-in types (excluding System.Object) may be declared as const. 只能将C#内置类型(不包括System.Object)声明为const。 For a list of the built-in types, see Built-In Types Table (C# Reference) . 有关内置类型的列表,请参见“ 内置类型表”(C#参考)

  • bool byte sbyte char decimal double float int uint long ulong object short ushort string bool字节sbyte char十进制double float int uint长ulong对象short ushort字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM