3 ms·
> Enums would demand an explicit cast enum Test { A,B,C } int x = Test.A; // This won't assign int x = (int)Test.A; // This wi
by louthy 13y ago
> Enums would demand an explicit cast
enum Test
{
A,B,C
}
int x = Test.A; // This won't assign
int x = (int)Test.A; // This will
> and they are static
Not sure why that's important.
> Also, any logic to them, would have to be added as extension methods
You can extend enums using extension methods, just like you can with classes or structs (you can also extend delegates FYI).
> and in the case I had, logic tied to the BookingTypeId was arround the corner.
No idea.
- klauserc 13y ago>> Enums would demand an explicit cast > > enum Test > { > A,B,C > } > > int x = Test.A; // This won't assign > > int x = (int)Test.A; // This will The OP wants the first one to work (that requires an implicit cast). The second one is an explicit cast. You explicitly demand a conversion to int. > > and they are static > > Not sure why that's important. You cannot add more values to the enum without modifying the enum. If you don't own code to the Assembly that defines the enum, you're out of luck. With the OP's solution, a third-party dev could add more 'constants' to "TypeId", simply by providing more factory methods. Sure those factory methods would not be located on the "TypeId" type, but that's acceptable. >> Also, any logic to them, would have to be added as extension methods > You can extend enums using extension methods, just like you can with classes or structs (you can also extend delegates FYI). Yes, but you can't add properties, for instance. Also extension methods are a C# and VB compiler feature. Other .NET languages (especially dynamic ones) will not discover these methods. Same goes for uses of 'dynamic' and reflection.
- louthy 13y agoYou seem to be shooting the messenger here? I'm not advocating any method, I was merely trying to help decipher the original message.