C#枚举作为函数参数? [英] C# enums as function parameters?

查看:999
本文介绍了C#枚举作为函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以通过一个标准的c#枚举作为参数吗?



例如:

 枚举e1 
{
// ...
}

枚举e2
{
// ...
}

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

public void myFunc(Enum e)
{
//通过e
中的所有值迭代

通过这样做,我希望在任何给定的枚举中检索所有的名称。

解决方案

这个!

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

foreach(名称中的名称)
{
//做某事!
}
}

编辑:我的坏,你说



注意:我知道我可以在我的foreach语句中进行GetNames()调用,但是我更喜欢将这种类型的事物分配给方法调用,因为它很方便调试。


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

For example:

enum e1
{
    //...
}

enum e2
{
    //...
}

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

public void myFunc( Enum e )
{
    // Iterate through all the values in e
}

By doing this I hope to retrieve all the names within any given enum. What would the Iteration code look like?

解决方案

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.

这篇关于C#枚举作为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆