Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 815
Hosam Aly Avatar asked Jan 04 '09 08:01

Hosam Aly


People also ask

Can strings be const?

String constants, also known as string literals, are a special type of constants which store fixed sequences of characters. A string literal is a sequence of any number of characters surrounded by double quotes: "This is a string." The null string, or empty string, is written like "" .

Can you change a const char in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

Can a char be passed as a const char?

Yes it is allowed. Using const in this case is good practice.

What does const char mean in C?

const char* is a pointer to a constant char, meaning the char in question can't be modified. char* const is a constant pointer to a char, meaning the char can be modified, but the pointer can not (e.g. you can't make it point somewhere else).


2 Answers

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.

like image 119
splattne Avatar answered Sep 30 '22 02:09

splattne


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.

like image 41
Jon Skeet Avatar answered Sep 30 '22 03:09

Jon Skeet