tags:

views:

2216

answers:

3

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?

+3  A: 

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? The latter will have almost the same semantics.

Jon Skeet
Not necessarily, a readonly is probably enough. They are both private, so it won't matter much. But I thought the compiler should be able to provide such operations implicitly. I also noticed that the opposite is also not possible! (i.e. initializing the char from the string)
Hosam Aly
+1  A: 

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 :)

Steven Robbins
+4  A: 

From the C# Language Specification (§ 17.3 and 14.16):

17.3 Constants

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

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.
  • 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.
  • 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).

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. Therefore you can use the ToString() method.

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.

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

splattne