Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum "Inheritance"

Tags:

c#

.net

enums

I have an enum in a low level namespace. I'd like to provide a class or enum in a mid level namespace that "inherits" the low level enum.

namespace low {    public enum base    {       x, y, z    } }  namespace mid {    public enum consume : low.base    {    } } 

I'm hoping that this is possible, or perhaps some kind of class that can take the place of the enum consume which will provide a layer of abstraction for the enum, but still let an instance of that class access the enum.

Thoughts?

EDIT: One of the reasons I haven't just switched this to consts in classes is that the low level enum is needed by a service that I must consume. I have been given the WSDLs and the XSDs, which define the structure as an enum. The service cannot be changed.

like image 628
CodeMonkey1313 Avatar asked Apr 16 '09 19:04

CodeMonkey1313


People also ask

What is enum inheritance?

Enums cannot inherit from other enums. In fact all enums must actually inherit from System. Enum . C# allows syntax to change the underlying representation of the enum values which looks like inheritance, but in actuality they still inherit from System. enum.

Can enum class be inherited?

Java Enum and Interface As we have learned, we cannot inherit enum classes in Java. However, enum classes can implement interfaces.

Is it possible to inherit enum in C#?

Nope. it is not possible. Enum can not inherit in derived class because by default Enum is sealed.

Can enum extend class?

Enum cannot extend any class in java,the reason is, by default Enum extends abstract base class java. lang. Enum. Since java does not support multiple inheritance for classes, Enum can not extend another class.


1 Answers

This is not possible. Enums cannot inherit from other enums. In fact all enums must actually inherit from System.Enum. C# allows syntax to change the underlying representation of the enum values which looks like inheritance, but in actuality they still inherit from System.enum.

See section 8.5.2 of the CLI spec for the full details. Relevant information from the spec

  • All enums must derive from System.Enum
  • Because of the above, all enums are value types and hence sealed
like image 135
JaredPar Avatar answered Sep 20 '22 16:09

JaredPar