I have an enum, let’s call it A:
public enum A
{
A,
B
}
I have a function that takes an enum A:
public void functionA(A enumA)
{
//do something
}
How can I create another enum, possibly call B that I can pass to functionA? Something like this?
public enum B
{
C
}
functionA(B.C);
I know that you can’t extend an enum, but what other options do I have available? What is the best way to achieve this?
You can’t.
Enum types are final by design.
The reason is that each enum type should have only the elements declared in the enum (as we can use them in a switch statement, for example), and this is not possible if you allow extending the type.
You might do something like this:
Note that it is not possible to do a
switchwith this interface, then. (Or in general have aswitchwith an object which could come from more than oneenum).