将定义值列表作为参数C#传递

时间:2017-11-19 02:39:28

标签: c#

如何让我的方法定义了可以传递的值列表。我在VB.net中看到了这个,但是无法在C#中找到它,它看起来不像enum

class Test
{
    List { active, all, completed}

    public string get(string a, List b)
    {
       // some code
    }

    string a = get("foo", active);
    string b = get("foo", all);
}

1 个答案:

答案 0 :(得分:4)

  

如果enum被称为测试,我需要通过test.active而我不想要那样。我只需要传递活动

当您想要使用枚举时,您可以使用using static关键字,然后您可以使用您想要的单词。

namespace Foo
{
    class Test
    {
        public string get(string a, List b)
        {
           // some code
        }
    }

    public enum List { active, all, completed}
}

一样使用
using Foo;
using static Foo.List;

public void Example()
{
    var test = new Test(); 
    //Because of "using static Foo.List;" you don't need to use "List.active"
    string a = test.get("foo", active);
    string b = test.get("foo", all);
}
相关问题