C# enums as function parameters?

后端 未结 7 1420
广开言路
广开言路 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:23

    Use the Enum.GetNames( typeof(e) ) method, this will return an array of strings with the names.

    You can also use Enum.GetValues to obtain the counterpart values.

    Edit -Whoops - if you are passing the parameter as Enum, you will need to use e.GetType() instead of typeof() which you would use if you had passed the parameter in as the actual Enum type name.

提交回复
热议问题