C# enums as function parameters?

后端 未结 7 1422
广开言路
广开言路 2021-01-04 00:10

Can you pass a standard c# enum as a parameter?

For example:

enum e1
{
    //...
}

enum e2
{
    //...
}

public void test()
{
    myFunc( e1 );
           


        
7条回答
  •  日久生厌
    2021-01-04 00:16

    This!

            public void Foo(Enum e)
            {
                var names = Enum.GetNames(e.GetType());
    
                foreach (var name in names)
                {
                    // do something!
                }
            }   
    

    EDIT: My bad, you did say iterate.

    Note: I know I could just do the GetNames() call in my foreach statement, but I prefer to assign that type of thing to a method call first, as it's handy for debugging.

提交回复
热议问题