Empowering Enums

added by KodefuGuru
9/21/2011 7:58:14 PM

430 Views

When reading Steve Smith’s articles on enums on ASP.NET Alliance and his blog, I couldn’t help but think of a different approach to the issue. I’ve seen, used, and coded the enum class pattern before (and it works well), but we have new language features today to encourage reuse and empower true enums. I am going to show you techniques that won’t require significant changes to your code if you have an existing base of enums.


3 comments

dpeterson
9/22/2011 9:11:11 AM
I like your approach, but what about handling the undefined enum Steve showed in his ShouldNowAllowInvalidValue test? I'm guessing his point was that undefined values didn't throw exceptions, whereas a class would most likely throw a NullReferenceException. Or was your article just focusing on adding custom attributes to the Enum?
Thanks for sharing!

KodefuGuru
9/22/2011 10:23:00 AM
Thanks dpeterson! To answer your question, you would do exactly what Steve did but with the extension methods. Check Enum.IsDefined and throw an exception if it isn't (if this is the desired behavior).

dpeterson
9/22/2011 10:31:14 AM
Thanks for the followup!