“Inheritance” of enums
In C#, enumerations cannot inherit from other enumerations. This is due to the way enumerations are implemented in the CLI, all enumerations must derive from the System.Enum base class.
Enumeration syntax that looks like it is inherited from another enumeration is actually a change in the way the underlying value of the enumeration is represented, not true inheritance. For example:
<code class="language-c#">namespace low { public enum Base { X, Y, Z } } namespace mid { public enum Consume : Base { // 隐式继承 Base 的值 } }</code>
This syntax can be misleading because it implies that Consume inherits from Base. However, in reality, Consume is still a separate enumeration that inherits from System.Enum.
This behavior is clearly defined in section 8.5.2 of the CLI specification:
Therefore, it is not possible in C# to create a class or enumeration that actually inherits the value of another enumeration.
The above is the detailed content of Can C# Enums Inherit from Other Enums?. For more information, please follow other related articles on the PHP Chinese website!