[英]Can I initialize a const string from a const char in C#?
I am trying to do the following in a way or another: 我试图以某种方式做以下事情:
const char EscapeChar = '\\';
const string EscapeString = EscapeChar.ToString(); // or ("" + EscapeChar)
This does't compile. 这不编译。 Is there another way to make it work? 有没有其他方法可以使它工作?
From the C# Language Specification (§ 17.3 and 14.16): 根据C#语言规范 (第17.3和14.16节):
17.3 Constants 17.3常数
A constant is a class member that represents a constant value: a value that can be computed at compile-time. 常量是表示常量值的类成员:可以在编译时计算的值。
and 和
14.16 Constant expressions 14.16常量表达式
A constant expression is an expression that shall be fully evaluated at compile-time . 常量表达式是一个在编译时应完全计算的表达式。 Where an expression is required to be constant this is indicated in the grammar by using constant-expression. 在表达式需要是常量的情况下,这通过使用常量表达式在语法中指示。 [...] The following constructs are permitted in constant expressions : [...] 常量表达式允许使用以下结构:
- Literals (including the null literal) 文字(包括空文字)
- References to const members of class and struct types. 对类和结构类型的const成员的引用。
- References to members of enumeration types. 引用枚举类型的成员。
- Parenthesized sub-expressions, which are themselves constant expressions. 带括号的子表达式,它们本身是常量表达式。
- Cast expressions, provided the target type is one of the types listed above. 如果目标类型是上面列出的类型之一,则转换表达式。
- The predefined checked and unchecked, +, –, !, and ~ unary operators. 预定义的已检查和未选中的+, - ,!和〜一元运算符。
- The predefined +, –, *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <=, and >= binary operators, provided each operand is of a type listed above. 预定义的+, - ,*,/,%,<<,>>,&,|,^,&&,||,==,!=,<,>,<=和> =二元运算符,每个都提供操作数是上面列出的类型。
- The ?: conditional operator. ?:条件运算符。
- sizeof expressions, provided the unmanaged-type is one of the types specified in §14.5.12. sizeof表达式,前提是unmanaged-type是§14.5.12中指定的类型之一。
- default value expressions, provided the type is one of the types listed above, or the type is a reference type or a type parameter that is known to be a reference type (§25.7). 默认值表达式,前提是类型是上面列出的类型之一,或者类型是引用类型或已知为引用类型的类型参数(第25.7节)。
The following conversions are permitted in constant expressions: 常量表达式允许以下转换:
- Identity conversions 身份转换
- Numeric conversions 数字转换
- Enumeration conversions 枚举转换
An other way to achieve what you want is to use a static readonly member. 实现您想要的另一种方法是使用静态只读成员。 Readonly members are evaluated at runtime, not at compile time. Readonly成员在运行时进行评估,而不是在编译时进行评估。 Therefore you can use the ToString() method. 因此,您可以使用ToString()方法。
private static readonly EscapeString = EscapeChar.ToString();
Note: Because a readonly field can be initialized either at the declaration or in the constructor of a class, readonly fields can have different values depending on the constructor used . 注意:因为readonly字段可以在声明或类的构造函数中初始化,所以readonly字段可以具有不同的值,具体取决于所使用的构造函数 。
Here is a good article about the differences between const and readonly members . 这是一篇关于const和readonly成员之间差异的好文章。
I don't see any way of doing it, which I agree is a bit of a pity - but do you really need it to be a const
instead of static readonly
? 我没有看到任何方式这样做,我同意这有点可惜 - 但你真的需要它是一个const
而不是static readonly
吗? The latter will have almost the same semantics. 后者将具有几乎相同的语义。
C#.Net const requires its value initialised at compile time. C#.Net const要求在编译时初始化其值。 That is the reason, your code is not compiling. 这就是原因,你的代码没有编译。 You can use readonly field to assign run time value. 您可以使用只读字段来指定运行时值。
However, following code will work: 但是,以下代码将起作用:
const char EscapeChar = '\\';
readonly string EscapeString = EscapeChar.ToString(); // or ("" + EscapeChar)
The only ways I can think of (both not ideal) are: 我能想到的唯一方法(两者都不理想)是:
const string EscapeString = "\\";
private static readonly EscapeString = EscapeChar.ToString();
Or you could just stick with the char version and ToString() it whenever you need the string version :) 或者,只要你需要字符串版本,你就可以坚持使用char版本和ToString()它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.