Home:ALL Converter>Why were Enums compiled as constants instead of static values?

Why were Enums compiled as constants instead of static values?

Ask Time:2011-12-30T12:29:01         Author:myermian

Json Formatter

I understand that Enums are compiled as constants, so changing them results in a breaking change. What I'm wondering is why weren't Enums compiled the same way static readonly variables are instead?

Author:myermian,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8676173/why-were-enums-compiled-as-constants-instead-of-static-values
Polity :

Both given answers are technically correct but miss the explanation for differantiating constants with statics (readonly). In C#, constants are always faster than readonly and the reason is very simple and best expressed with a small code example:\n\nconst int MyConstant = 10;\nstatic readonly int MyReadonly = 20;\n\nstatic void Main()\n{\n int result = MyConstant + MyReadonly;\n // the above statement will be resolved to:\n // int result = 10 + MyReadonly\n}\n\n\nAt compile time, the compiler replaces all references to a constant with the actual value of that constant. It is able to do so because a constant must be predefined at compile time. This differs from static readonly values which although static, are actually resolved at runtime. Take the following example:\n\nstatic readonly Encoding = Encoding.GetEncoding(\"GB2132\");\n\n\nThere is no way for the compiler to know if GB2132 actually exists on the machine on which this code is intended to run. The only way to resolve this value is at runtime. Static ensures that the value itself is not bound to the lifetime of an instance and readonly ensures that the value can only be set once. There is no way for the compiler to replace references to this field at compile time since the value simply can't be known.\n\nLogically, only primitive types can be marked as constant. \n\nNow in case of enums, it's very simple. Enums are nothing more than integer value's with a label. So the following code:\n\nenum MyEnum\n{ \n First,\n Second,\n Third\n}\n\nstatic void Main()\n{\n MyEnum test = MyEnum.First;\n\n if (test == MyEnum.Second)\n {\n // whatever\n }\n}\n\n\nWill be resolved by the compiler to:\n\nconst int MyEnum_First = 0;\nconst int MyEnum_Second = 1;\nconst int MyEnum_Third = 2;\n\nstatic void Main()\n{\n int test = MyEnum_First;\n\n if (test == MyEnum_Second )\n {\n // whatever\n }\n}\n\n\nWhich in turns means the actual references to constant fields can be replaced with the value known at compile time, making the final version of the code something like:\n\nstatic void Main()\n{\n int test = 0;\n\n if (test == 1 )\n {\n // whatever\n }\n}\n",
2011-12-30T06:03:36
keyboardP :

They can be more efficient than fields, so there's no need to compile them in the same way when they can be directly embedded into the IL. \n\n\n [Enums] are loaded in the same\n way that const value are loaded. They are embedded directly into the\n IL. Fields, on the other hand, require a field load instruction\n (ldsfld) which will impact performance somewhat. Enums are therefore\n just as fast as const in typical usage; fields are somewhat slower.\n\n\n(Source)",
2011-12-30T04:34:39
yy