65.9K
CodeProject is changing. Read more.
Home

Passing Enum type as a parameter

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Sep 5, 2011

CPOL
viewsIcon

18100

I think the start is wrong... we can pass types as parameters.A method like this:void ShowName(Type type){ Console.WriteLine(type.FullName);}can be called as:ShowName(typeof(object));or as this:ShowName(someObject.GetType());If you are trying to say that we can't...

I think the start is wrong... we can pass types as parameters. A method like this:
void ShowName(Type type)
{
  Console.WriteLine(type.FullName);
}
can be called as:
ShowName(typeof(object));
or as this:
ShowName(someObject.GetType());
If you are trying to say that we can't pass enum types as type parameters, I will partially agree, as we can't use where T: enum. But, you say that generic methods solve the problem, so that's not the case. The only advantage (if that's really to be considered one) about using generic methods to pass types is that null or open generic types (like List<> without specifying a data type) will never be received. But doing the necessary ifs are probably faster than counting with the generation of the method.