Reputation: 6744
Basically as stated in the title, if I do this:
private const string TYPEOF_STRING = typeof(String).FullName;
Why does it give me the error of:
The expression being assigned to 'Cognitronics.Generic.CloudClient.TYPEOF_STRING' must be constant
EDIT:
It seems we have conflicting answers here and in the thread Habib linked too. Here everyone has said that it is not a compile time constant, whereas in the other thread everyone says it is. This has just made me even more confused so I would like to re-ask which is it?
Upvotes: 3
Views: 469
Reputation: 21742
typeof(string)
returns an object of type System.Type
and you are accessing a property of that object. Objects don't exist at compile time, so the value of their properties can't be determined to be a constant at compile time. Fullname
being an abstract property also mean that there's no way to ensure that Fullname is a constant. So even knowing that the first expression will always yield the same result is not enough to determine that the value of Fullname is a constant
The below is the IL produced for typeof(int)
ldtoken int32
call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)
which includes a call to a method and therefore is clearly not constant.
Upvotes: 0
Reputation: 11721
Const means it is constant at compile time and your code needs to run
Use readonly if you want it at run time.
As stated in official site :-
although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;
And you have just learned one frequently asked interview question ;)
Upvotes: 1
Reputation: 43254
To use the const
keyword, the compiler must be able to compute the value assigned at compile time. If it's computed at run time, then the readonly
keyword should be used.
Both are constants, in so far as the value cannot be changed.
So to declare your constant, use:
private readonly string typeOfString = typeof(String).FullName;
Upvotes: 0
Reputation: 14672
The value of
typeof(String).FullName
is not a compile time constant, code has to execute to know this value.
Upvotes: 13