There is one thing you should watch out for when using enumerations. You can cast any integer to an Enum type and you will not get an exception message. Therefore if the value comes from an external source (for example, from user input) check it against an enum before casting. The easiest way to do it is to use Enum.IsDefined method:
int submitedValue = 123;
if (Enum.IsDefined(typeof(MyEnum), submitedValue))
{
//do casting here
}
Note: because Enum.IsDefined loads reflection it should be used with caution because of its performance penalty.
via M. Parreira
Your article looks good. Can you explain me some real time example of using this situation?
Madhan 11/26/2007 12:16:14 PM
For example, when you reading data from external database/file and converting it into variable of enum type, don't rely on exception being thrown to catch invalid data.
kostya.ly 11/27/2007 1:30:10 PM