1

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?

Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
  • 2
    The compiler executes the `+` concatenation when all parts of the concatenation are constant, the `string.Concat()` and string interpolation happen at runtime, therefore can't be constant. See duplicate and other questions on the same subject. – CodeCaster Aug 13 '20 at 09:12
  • Thanks @CodeCaster. I think that answers the question but I don't think my question is a duplicate of the referenced question. That relates to performance and whilst the answer may be the same the question is not. – Philip Stratford Aug 13 '20 at 09:15
  • 2
    This is a case of "because the language says so" -- although the compiler can optimize this particular case of interpolation and `String.Concat` to exactly the expression `hello + " " + world`, this would not apply in general, as that can involve arbitrary expressions. It would be impractical (for the compiler writers) to allow interpolation, but only where the compiler happens to optimize it in a manner that doesn't conflict with constant evaluation. This is doubly true for `String.Concat`, which is just a method call where the language dictates no semantics. – Jeroen Mostert Aug 13 '20 at 09:16
  • 1
    The second question I added also doesn't ask the same question as yours, but the answers to both do answer yours. I'm trying to find better duplicates, am sure it's been asked literally like this before. – CodeCaster Aug 13 '20 at 09:16
  • 1
    [FYI](https://sharplab.io/#v2:D4AQTAjAsAUCDMACciDCiDetE+UgxgPYB2AzgC7IQAMiAFgKYA2ThiAvIgEQASzrXANzZcCRETKUQNRAHdCAJyYATDt3lLlQkTjESKVWoxaEA6opVrjrRAGpuD+xpXCYuRDrzIALIgAihADKhAC2DOR0AJbEAOYAFIHkCtExiACGAEb4AJSeWG7uuAHBYREpcdZmFsrZru4AvrD1QA==) – ProgrammingLlama Aug 13 '20 at 09:22
  • @CodeCaster I agree, but respectfully I don't see a problem with different questions that have the same answer. If they were similar enough to my question I'd have found them when I did a search before posting my own. Just because `1+1` has the same answer as `4-2` doesn't make either question a duplicate. :) – Philip Stratford Aug 13 '20 at 09:23
  • 1
    The answer to _"why?"_ is [because the specs say so](https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#constant-expressions). Why duplicate answers when you can point different questions to the same answer? :) I think I've found the best duplicate, and moved it to the top. Agreed? – CodeCaster Aug 13 '20 at 09:29
  • @CodeCaster Because when you're trying to find a solution you search for similar *questions* to your own. You won't necessarily find the answer if it's in response to a different question. Anyway, if everyone feels the same way as you I'll happily delete my question. This feels more like a debate for Meta, and I'm sure it's been had before! – Philip Stratford Aug 13 '20 at 09:33
  • 1
    @CodeCaster: That said, I disagree either of these questions is a proper duplicate -- one asks for performance, the other whether the compiler optimizes. Neither deal with the central issue of why compiler-optimized constant expressions aren't allowed as actual constant expressions, and even if the answer really is a trivial "because the spec says so", that's still a different answer. The fact that the compiler does optimize both of these to what is *effectively* a constant expression adds a twist, in any case. – Jeroen Mostert Aug 13 '20 at 09:33
  • Definitely don't delete the question, your wording will now be indexed by the search engines and link to the common answer! :) @Jeroen I admitted that and said I was searching, do you agree the top listed duplicate _is_ in fact a proper dupe? The problem is that not enough questions are closed as dupe, and for various variations of the search terms I've used, dozens of slightly related questions with about the same answers were posted, but none were _exhaustive_. – CodeCaster Aug 13 '20 at 09:33
  • @CodeCaster: It is, although the accepted answer fails to address the complete issue as well. Fortunately there's another answer that links to an issue on Roslyn that does include a more or less complete discussion of the topic. – Jeroen Mostert Aug 13 '20 at 09:38

0 Answers0