Does C# optimize the concatenation of string literals?

Yes. This is guaranteed by the C# specification. It’s in section 7.18 (of the C# 3.0 spec):

Whenever an expression fulfills the
requirements listed above, the
expression is evaluated at
compile-time. This is true even if the
expression is a sub-expression of a
larger expression that contains
non-constant constructs.

(The “requirements listed above” include the + operator applied to two constant expressions.)

See also this question.

Leave a Comment