Compiler error CS0283 indicates that only the basic POD types (as well as strings, enums, and null references) can be declared as const
. Does anyone have a theory on the rationale for this limitation? For instance, it would be nice to be able to declare const values of other types, such as IntPtr.
I believe that the concept of const
is actually syntactic sugar in C#, and that it just replaces any uses of the name with the literal value. For instance, given the following declaration, any reference to Foo would be replaced with "foo" at compile time.
const string Foo = "foo";
This would rule out any mutable types, so maybe they chose this limitation rather than having to determine at compile time whether a given type is mutable?
The C programming language doesn't seem to have an expiration date. It's closeness to the hardware, great portability and deterministic usage of resources makes it ideal for low level development for such things as operating system kernels and embedded software.
After language 'B', Dennis Ritchie came up with another language which was based upon 'B'. As in alphabets B is followed by C and hence he called this language as 'C'.
As of July 2022, the most recent stable version of the language is C# 10.0, which was released in 2021 in . NET 6.0.
C has a stable ABI (Application Binary Interface) increasing compatibility between different compilers. 8. C is somewhat more efficient than C++ since it doesn't need for Virtual Method Table (VMT) lookups. VMT — It is a mechanism used in programming languages to support dynamic dispatch (or Runtime Method Binding).
From the C# specification, chapter 10.4 - Constants:
(10.4 in the C# 3.0 specification, 10.3 in the online version for 2.0)
A constant is a class member that represents a constant value: a value that can be computed at compile time.
This basically says that you can only use expressions that consists solely of literals. Any calls to any methods, constructors (that cannot be represented as pure IL literals) cannot be used, as there is no way for the compiler to do that execution, and thus compute the results, at compile time. Also, since there is no way to tag a method as invariant (ie. there is a one-to-one mapping between input and output), the only way for the compiler to do this would be to either analyze the IL to see if it depends on things other than the input parameters, special-case handle some types (like IntPtr), or just disallow every call to any code.
IntPtr, as an example, though being a value type, is still a structure, and not one of the built-in literals. As such, any expression using an IntPtr will need to call code in the IntPtr structure, and this is what is not legal for a constant declaration.
The only legal constant value type example I can think of would be one that is initialized with zeroes by just declaring it, and that's hardly useful.
As for how the compiler treats/uses constants, it will use the computed value in place of the constant name in the code.
Thus, you have the following effect:
if (SomeClass.Version == 1)
, when SomeClass.Version has the value of 1, will in fact remove the if-statement, and keep the block of code being executed. If the value of the constant is not 1, then the whole if-statement and its block will be removed.In other words, with the following scenario:
In this case, assembly B, in its compiled form, will still compare the value of 1 to 1, because when B was compiled, the constant had the value 1.
In fact, if that is the only usage of anything from assembly A in assembly B, assembly B will be compiled without a dependency on assembly A. Executing the code containing that expression in assembly B will not load assembly A.
Constants should thus only be used for things that will never change. If it is a value that might or will change some time in the future, and you cannot guarantee that all other assemblies are rebuilt simultaneously, a readonly field is more appropriate than a constant.
So this is ok:
while this is not:
Edit May 27th 2016
OK, just got an upvote, so I re-read my answer here and this is actually slightly wrong.
Now, the intention of the C# language specification is everything I wrote above. You're not supposed to use something that cannot be represented with a literal as a const
.
But can you? Well, yes....
Let's take a look at the decimal
type.
public class Test
{
public const decimal Value = 10.123M;
}
Let's look at what this class looks like really when looked at with ildasm:
.field public static initonly valuetype [mscorlib]System.Decimal X
.custom instance void [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(int8, uint8, uint32, uint32, uint32) = ( 01 00 01 00 00 00 00 00 00 00 00 00 64 00 00 00 00 00 )
Let me break it down for you:
.field public static initonly
corresponds to:
public static readonly
That's right, a const decimal
is actually a readonly decimal
.
The real deal here is that the compiler will use that DecimalConstantAttribute
to work its magic.
Now, this is the only such magic I know of with the C# compiler but I thought it was worth mentioning.
Does anyone have a theory on the rationale for this limitation?
If it's allowed to be just a theory, my theory is that const values of primitive types can be expressed at literal opcode parameters in the MSIL ... but the values of other, non-primitive types cannot, because MSIL doesn't have the syntax to express the value of a user-defined type as a literal.
I believe that the concept of const is actually syntactic sugar in C#, and that it just replaces any uses of the name with the literal value
What does the compiler do with const objects in other languages?
You can use readonly for mutable types that me be evaluated at runtime. See this article for the differences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With