Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Passing Enum type as a parameter

0.00/5 (No votes)
2 Oct 2011 2  
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:
C#
void ShowName(Type type)
{
  Console.WriteLine(type.FullName);
}


can be called as:
C#
ShowName(typeof(object));


or as this:
C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here