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.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Possible duplicate of [What is the difference between const and readonly?](http://stackoverflow.com/questions/55984/what-is-the-difference-between-const-and-readonly) – AlphaModder Oct 11 '15 at 09: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. – Konrad Viltersten Oct 11 '15 at 09: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. – Ganesh R. Oct 11 '15 at 09:53
  • @GaneshR. You should put that as a reply not comment. – Konrad Viltersten Oct 11 '15 at 09:58

2 Answers2

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?

Community
  • 1
  • 1
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
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' });
Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • 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. – Konrad Viltersten Oct 11 '15 at 09:49
  • Added some explanation and example, does that help you understand? – Arghya C Oct 11 '15 at 09:56
  • 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. – Konrad Viltersten Oct 11 '15 at 09:59
  • That's fine, wait accept the answer that helps you the most :) – Arghya C Oct 11 '15 at 10:01