c# Enum Function Parameters

TK. picture TK. · Feb 3, 2009 · Viewed 14.3k times · Source

As a follow on from this question.

How can I call a function and pass in an Enum?

For example I have the following code:

enum e1
{
    //...
}

public void test()
{
    myFunc( e1 );
}

public void myFunc( Enum e )
{
    var names = Enum.GetNames(e.GetType());

    foreach (var name in names)
    {
        // do something!
    }

}

Although when I do this I am getting the 'e1' is a 'type' but is used like a 'variable' Error message. Any ideas to help?

I am trying to keep the function generic to work on any Enum not just a specific type? Is this even possible?... How about using a generic function? would this work?

Answer

Martin Moser picture Martin Moser · Feb 3, 2009

Why not passing the type? like:

 myfunc(typeof(e1));

public void myFunc( Type t )
{
}