0

Please note that I'm not asking about the difference between const, readonly and static. I assumed that it was clear from the contents of the question but apparently I didn't succeed to make that clear enough.

I'm aware that when declaring a constant using the keyword const, I have to specify a value at compile time and that the value needs to be, well..., constant.

The following sample works but I found that a bit lengthy and unnecessarily relaxed, so I attempted to declare the field as constant. According to my estimation, we do have a constant specification of the contents and those are never changing.

static readonly List<int> Codes = new List<int> { 1337 }; 

Evidently, according to the computer it's not and the nit-picker won't compile the following example. That contradicts my expectations and I'm not clear on how the computer figures. Hence the question - why doesn't it like it?

const List<int> Codes = new List<int> { 1337 }; 

The exact formulation is, as one'd expect: Constant initializer must be compile-time constant but that answers why the error. It doesn't really explains where's the non-constant part.

4
  • Possible duplicate of What is the difference between const and readonly? Commented Oct 11, 2015 at 9:48
  • @AlphaMCubed Care to point out where that link refers to my question? I can google "const vs readonly" too but it doesn't give me understanding for my confusion. Perhaps I'm just blinded by the unexpected. Commented Oct 11, 2015 at 9:53
  • From what I know, whatever is declared as constant, the compiler just replaces the reference to that variable with its value in place. I don't think this can be done for a List as the reference of the list will not change but the same is not true for the values in it. Commented Oct 11, 2015 at 9:53
  • @GaneshR. You should put that as a reply not comment. Commented Oct 11, 2015 at 9:58

2 Answers 2

3

const has to be compile-time constant because its value will be embeded in the emitted Intermediate Language (IL) code.

Here new List<int> { 1337 }; is an instruction that is run at runtime. The compiler cannot embed this new List<int> { 1337 }; in the IL without running the code to actually create the list.

For more information: How to stop C# from replacing const variable with their values?

Sign up to request clarification or add additional context in comments.

Comments

2

const has to be compile-time constant. If you do new Something() that is not. See MSDN reference here.

But you can declare that as static as you have seen, which need not be a constant value.

Now, the field value has to be compile-time-constant means it's value must be known and constant at compile time itself. But when you call the constructor of a class, the actual object will be created at run-time, so that is not known for compiler.

Example:

const string Allowed = "hello!"; const string NotAllowed = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); 

4 Comments

I'm not contradicting you. I just can't see how it's not constant - we have a memory allocation for the array, it's not going to change, it's not going to span over a larger or smaller chunk of the memory. How?! I'm too dense to see that... +1 for the lightning speed of the response.
Added some explanation and example, does that help you understand?
Yupp. Combined with the other comments/replies I see where my brains went poof. I can't accept your reply yet, though. Need to wait for a few minutes.
That's fine, wait accept the answer that helps you the most :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.