English 中文(简体)
在c#中,如果某一数值是按条形编码的一组,那么是否容易测试?
原标题:In c# is there an easy way to test if a value is in an inline-coded set?

In C# if I want to see if a variable is equal to one of a set of fixed values, I currently do this...

bool result = ( (x==MyEnum.A) || (x==MyEnum.B) || (x==42) );

...which to me is cumbersome. Is there anything similar to this pseudo-code?

bool result = x in {MyEnum.A, MyEnum.B, 42};

我知道,我可以建立一个具有价值观的阵列,然后使用准则,但是否有任何其他选择?

最佳回答

This is probably a BAD idea, but you can turn Contains on its head and get syntax similar to what you ask in your question with a new extension method:

//Generic.  All items in the set and the candidate must be the same type.
public static bool In<T>(this T item, params T [] set)
{
  return set.Contains(item);
}

bool result = x.In(MyEnum.A, MyEnum.B, MyEnum.C);

//Non-generic and non-typesafe.  Anything goes.  Use with care!
public static bool In(this object item, params object [] set)
{
  return set.Contains(item);
}


bool result = x.In(MyEnum.A, MyEnum.B, 42);

//int-specific.  
public static bool In(this int item, params int [] set)
{
  return set.Contains(item);
}


bool result = x.In((int)MyEnum.A, (int)MyEnum.B, 42);
问题回答

Using LINQ, you can reverse this (test if x is in a list) by using Contains:

var myList = new[] {MyEnum.A, MyEnum.B, (MyEnum)42};
result = myList.Contains(x);
var result = new[] { (int)MyEnum.A, (int)MyEnum.B, 42 }.Contains(x);

甚至更好:

3. 建立推广方法:

    public static bool ContainedIn(this MyEnum input, params MyEnum[] parameters)
    {
        return parameters.Contains(input); 
    }

var results = x.ContainedIn(MyEnum.A, MyEnum.B);

并且如果你希望允许多种类型,你可以把物体当作准物。

你可以利用数字。 1. 具备的所有可计算类型:

public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value);

The syntax is still a little ugly, but you can get close to what you want:

public enum MyEnum
{
    A, B, C
}
public static void Test()
{
    MyEnum x = MyEnum.C;
    bool result = new[] { MyEnum.A, MyEnum.B, (MyEnum)42 }.Contains(x);
}

If your list is too long but values you look for is ordered in your enum definition, you can create a range and call Contains() on it like this:

bool result = Enumerable.Range((int)MyEnum.First,(int)MyEnum.Last)
                        .Contains((int)x);

这将是在以上条件下进行的,但我要说,这是因为你不想建立一线阵列。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签