Is it possible to initialize a const string from const char in C #?

I am trying to do the following one way or another:

const char EscapeChar = '\\'; const string EscapeString = EscapeChar.ToString(); // or ("" + EscapeChar) 

This does not compile. Is there any other way to make it work?

+6
string c # const
source share
4 answers

From the C # language specification (§17.3 and 14.16):

17.3 Constants

A constant is a member of a class that is a constant value: values ​​that can be computed at compile time.

and

14.16 Constant Expressions

A constant expression is an expression that must be fully evaluated at compile time. If the expression requires constancy, it is specified in the grammar using the constant expression. [...] The following constructions are allowed in constant expressions:

  • Literals (including Zero Literal)
  • References to constant elements of classes and types.
  • References to members of enumeration types.
  • Parenthesized sub-expressions, which themselves are constant expressions.
  • Assignment of expressions provided that the target type is one of the types listed above.
  • Predefined verified and unverified, +, - ,! and ~ unary operators.
  • Predefined +, -, *, /,%, <, →, &, |, ^, &, ||, == ,! =, <,>, <; =, and> = binary operators if each operand is of the type specified above.
  • The operator ?: conditional.
  • sizeof the expression if the unmanaged type is one of the types specified in clause 14.5.12.
  • the default value of the expression, if the type is one of the types listed above, or the type is a reference type or a parameter type, which is known to be a reference type (§25.7).

The following conversions are allowed constant expressions:

  • Personality transformations
  • Numerical conversions
  • Transition Enumerations

Another way to achieve what you want is to use the static readonly member. Readonly members are evaluated at runtime, not at compile time. Therefore, you can use the ToString () method.

 private static readonly EscapeString = EscapeChar.ToString(); 

Note. . Because the readonly field can be initialized either in the declaration or in the constructor of the class, readonly fields can have different values ​​depending on the constructor used.

Here is a good article on the differences between const and readonly members .

+7
source share

I see no way to do this, which I agree is a bit sorry, but do you really need it to be const instead of static readonly ? The latter will have almost the same semantics.

+5
source share

The only ways I can think (both are not perfect):

 const string EscapeString = "\\"; private static readonly EscapeString = EscapeChar.ToString(); 

Or you can just stick with the char version and ToString () when you need the string version :)

+1
source share

C # .Net const requires that its value be initialized at compile time. This is why your code does not compile. You can use the readonly field to assign a runtime value.

However, the following code will work:

 const char EscapeChar = '\\'; readonly string EscapeString = EscapeChar.ToString(); // or ("" + EscapeChar) 
+1
source share

All Articles