In C# it's possible to get the number of elements in an enum using this code:
int numberOfElements = Enum.GetNames(typeof(MyEnum)).Length;
How can this code be put into it's own function, be it a normal function, a static one, extension or a generic, so that the call to it can be simplified to something like:
int numberOfElements = GetEnumEntries(MyEnum); // something like this
int numberOfElements = GetEnumEntries<MyEnum>(); // or this
For instance if I try this:
static public int GetEnumEntries(System.Type t) {
int numberOfElements = System.Enum.GetNames(typeof(t)).Length;
}
I get the error 'the type or namespace t could not be found'