In C#, this is acceptable:
public const string hello = "Hello";
public const string world = "world";
public const string helloWorld = hello + " " + world;
However, if I try either of the following:
public const string helloWorld = string.Concat(hello, " ", world);
or
public const string helloWorld = $"{hello} {world}";
I get the compile time error "The expression being assigned to helloWorld must be constant".
I understand the error and I know I can use public static readonly string
when declaring the helloWorld
variable instead, but to my mind the second and third methods are doing exactly the same thing as the first method, so why are they not allowed whilst the first method is?