Question Details

No question body available.

Tags

c# string char concatenation constants

Answers (1)

June 14, 2026 Score: 1 Rep: 121,846 Quality: Medium Completeness: 100%

Answering in reverse order:

  • Question #2: (for bonus points)
    What part of the standard am I violating here?

As of C# 14 / .NET 10, the problem seems to be that, in order to concatenate a string with a char it is first necessary to convert the char to a string via ToString(), which is not allowed in a constant expression. For confirmation, start with 12.13.5 Addition operator

UTF-16 string concatenation:

string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);

These overloads of the binary + operator perform concatenation of UTF-16 strings.... any non-string operand that is not a ref struct (§16.2.3) is converted to its UTF-16 string representation by invoking the virtual ToString method inherited from type object.

I.e. "" + c is actually defined to be "" + ((object)c).ToString().

Unfortunately this conversion seems not to be one of the permitted conversions as defined by 12.26 Constant expressions:

The following conversions are permitted in constant expressions:

  • Identity conversions
  • Numeric conversions
  • Enumeration conversions
  • Constant expression conversions
  • Implicit and explicit reference conversions, provided the source of the conversions is a constant expression that evaluates to the null value.

Note: Other conversions including boxing, unboxing, and implicit reference conversions of non-null values are not permitted in constant expressions. end note

That Note seems to rule out ToString() conversions. This is not entirely surprising because, in general, ToString() conversions of constants values are not necessarily constant themselves, they can be culture-dependent. Thus for instance if I do

readonly static int i = -1;
readonly static string istring = "" + i;

Then the resulting string will depend on the current culture, specifically CultureInfo.CurrentCulture.NumberFormat.NegativeSign which is not always the standard Ux002D HYPHEN-MINUS. For instance, in the Swedish culture it's U+2212 MINUS SIGN, see here for a demo.

Now of course char.ToString() isn't culture specific, so Microsoft could in theory have added it to the list of permitted constructs in constant expressions (or alternatively added an explicit override to the + operator for adding strings and characters that avoids boxing, then added that to the list of allowed constructs.) But, to quote Eric Lippert in this answer:

Features are unimplemented by default; C# does not have that feature because no one designed, implemented and shipped the feature to customers.

This just seems to be a reasonable feature that they haven't implemented yet.

Finally, with regards to using const char values in interpolated strings:

const string s = $"{c}";

This is explicitly disallowed in the doc page The const keyword:

Interpolated strings can be constants if all expressions used are also constant strings.

  • Question #1:
    How can I actually achieve what I am trying to do here, which is to embed a const char in a const string?

Unfortunately there doesn't seem to be a way to do this out-of-the-box in C# 14 / .NET 10, any more so than embedding a const int or a const double in a string. You may need to consider workarounds, such as using readonly static fields or making your char constants be strings and exposing their first character as a readonly property:

private const string cstring = "c";
public static char C => cstring[0];

Alternatively, if you need to deal with this in many places, as suggested by Alexander Petrov in comments you might look into creating a custom C# source generator that generates a corresponding const string for every const char.