-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Nested enums #14720
Comments
Barely useful, as you can write
and with intellisense you get the same benefits. Beside of this, CLR probably do not allow for these enhancements, and here must be implemented significant trickery to smoothly move these enums between sources and libraries. |
Well, the intellisense benefits are of no use to me, since I wanted to react differently based on the enum's value and "parent value" at runtime. This is the use case very similar to what I have in mind, and yes the solutions are "ugly" with "muddy intent". As for the CLR not allowing it smoothly, that I can understand. |
You could achieve this in F# with discriminated unions, but I would love to see this made possible in C# also |
Check out #6739. Normal C#/CLR enums can only be simple integral primitive types. Short of getting funky with bitmasks I don't see how a hierarchical relationship could be established through them. Discriminated unions would be a better feature. |
This can be represented by flags enum reasonably well [Flags]
enum Animal
{
//Groups
Mammal = 0x1,
Bird = 0x2,
Fish = 0x4,
//Mammals
Cat = 0x101,
Dog = 0x201,
Bear = 0x401,
//Birds
Pigeon = 0x102,
Hawk = 0x202,
Ostrich = 0x402
//Fishes
Goldfish = 0x104,
Trout = 0x204,
Shark = 0x404,
}; You can check for presence in group by calling |
This looks very similar to #13192. |
There is a similar request in dotnet/csharplang#587 |
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! |
I would find it convenient to have something like this (contrived example):
Sometimes I want the class hierarchy but sometimes I really don't. Use case would be something like this:
var creature1 = Animal.Bird.Hawk;
Animal creature2 = Animal.Mammal; //OK
creature1 == Animal.Bird; // false
creature1 is Animal.Bird; // true
creature1 is Animal; // true
The text was updated successfully, but these errors were encountered: