public void DeclareDayIsSpecial(DayOfWeek day) { if (day == DayOfWeek.Sunday || day == DayOfWeek.Saturday) throw new ArgumentException("When declaring that a day is special, the day must be a weekday.", "day"); Console.WriteLine("{0} is special.", day); }
Even though you can't define an enum as a subclass of another and allow only that subclass, you can define an enum as subset of another.
public enum Weekday { Monday = DayOfWeek.Monday, Tuesday = DayOfWeek.Tuesday, Wednesday = DayOfWeek.Wednesday, Thursday = DayOfWeek.Thursday, Friday = DayOfWeek.Friday, }
Once you have this you can constrain your methods to just the valid subset of values.
public void DeclareDayIsSpecial(Weekday weekday) { Console.WriteLine("{0} is special.", weekday); }
The compiler won't allow you to use an invalid value so you have compile time documentation and feedback and can remove the runtime checks. This can be useful if you use enums in a scenario where only some values are valid. And since enums are wrappers around primitive types, you can safely cast from the subset (
Weekday
) to the superset (DayOfWeek
).