C# - are all Enum constants?

后端 未结 2 450
深忆病人
深忆病人 2021-01-04 08:56

Are all Enum enumerations constants? Do they get converted to their value at compile-time, or at run-time?

2条回答
  •  不知归路
    2021-01-04 09:31

    They are constant. Yes, compile time.

    Reference: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

    From the intro:

    The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

    Under "Robust Programming":

    Just as with any constant, all references to the individual values of an enum are converted to numeric literals at compile time. This can create potential versioning issues as described in Constants (C# Programming Guide).

提交回复
热议问题