Tip 90 of #TuesdayCodingTips - Is enum value defined?
In both C++ and C#, enums are mere named numerical constants. Even if they appear to be distinct types, and switch statements can check whether you covered all names within an enum, that's simply not enough.
Anybody can take a random number and cast it to the enum's type, effectively crafting something that appears legit, but may fall through non-defaulted switch statements. While in C++ you don't have a foolproof defense against this (perhaps with C++26 reflection, we will), in C# you can easily check enum validity with `Enum.IsDefined` method.
It simply checks whether the current value of an enum has a name associated with it and if yes, it returns true. This is great, especially for the cases where values associated with underlying names aren't always incremented by one.