Consider the following code
enum HorizontalAlignment { Left, Middle, Right };
enum VerticleAlignment { Top, Middle, Bottom };
function OutputEnumValues (Type enumType)
{
foreach (string name in Enum.GetNames(typeof(enumType)))
{
Console.WriteLine(name);
}
}
Which can be called as
OutputEnumValues (typeof(HorizontalAlignment));
OutputEnumValues (typeof(VerticleAlignment ));
But I could inadvertently call, for example
OutputEnumValues (typeof(int));
And this will compile, but the runtime crashes in Enum.GetNames ()
Any way to write a method signature to catch this problem at compile time - that is, only accept enumeration types in OutputEnumValues?
Ryan source
share