c#:如果int x是给定集合的元素,最优雅的测试方法是什么?

时间:2021-01-15 20:19:29

Problem: Test if x ∉ { 2, 3, 61, 71 }

问题:测试如果x∉{ 2、3、61、71 }

I often wondered if there is not a better way than:

我常常想,如果没有比:

if (x != 2 && x != 3 && x != 61 && x != 71)
{
  // do things
}

and

if (!new List<int>{ 2, 3, 61, 71 }.Contains(x))
{
  // do things
}

The latter one seems quite elegant, but actually it is kind of irritating if you read it, especially because of the inversion. It's kind of an ugly thing because in English we say "x is not element of ...", which is hard to express in C# without irritating overhead. Maybe one coud say if (Object(x).IsElementOf(new[] { ... })) or so?

后者看起来很优雅,但实际上如果你读它的话会觉得有点烦人,尤其是因为倒装。这是一件很丑陋的事情,因为在英语中我们说“x不是……”在c#中,如果没有烦人的开销,就很难用c#来表达。可能有人会说if (Object(x))IsElementOf(新[]{…}))吗?

Hmm.. any suggestions? Are there any .Net standard methods to test things like that?

嗯. .有什么建议吗?有什么。net标准的方法可以测试这样的东西吗?

6 个解决方案

#1


42  

I use an extension method:

我使用扩展方法:

using System.Linq;

...

public static bool In<T>(this T item, params T[] list)
{
    return list.Contains(item);
}

...


if (!x.In(2,3,61,71))
...

You can rename it to IsElementOf if you prefer this name...

你可以把它重命名为IsElementOf如果你喜欢这个名字…

#2


5  

You could use following LinQ method:

您可以使用以下LinQ方法:

var list = new List<int> { 1, 2, 3, 4, 5 };
var number = 3;

if (list.Any(item => item == number))
    //number is in the list

And for the readability you can put it in an extension method:

对于可读性,你可以把它放在扩展方法中:

public static bool IsElementOf(this int n, IEnumerable<int> list)
{
    return list.Any(i => n == i);
}

//usage
if(3.IsElementOf(list)) //in the list

#3


5  

Old question, but haven't seen this simple answer:

老问题,但没见过这个简单的答案:

!new []{2, 3, 61, 71}.Contains(x)

#4


1  

what about

是什么

if(new[] { 2, 3, 61, 71 }.Except(x).FirstOrDefault() != 0)
{
   ...
}

or something on those lines?

或者是那些线条上的东西?

#5


0  

var list=CreateNewList(); //returns your list of elements
var element=GetElement(); //returns an element that might be in the list
if(list.Any(x=>x.Equals(element))
{
  //do something
}

It's still inverted from what you're used to but it's more expressive (if the list has any value that equals element).

它仍然与您所习惯的相反,但是它更有表现力(如果列表中有任何值等于元素)。

#6


0  

Assuming you meant && and not ||, you can just write a func and use that throughout your code. You can shorten the new[] part since the type (int) is infered by the in paramter of the func.

假设您的意思是&而不是||,那么您可以编写一个func并在整个代码中使用它。可以缩短新的[]部分,因为类型(int)是由func的in paramter推断的。

Func<int, bool> IsSafe = x => !new[] { 2, 3, 61, 71 }.Contains(x);

Console.WriteLine(IsSafe(68)); // is true
Console.WriteLine(IsSafe(2));  // is false

#1


42  

I use an extension method:

我使用扩展方法:

using System.Linq;

...

public static bool In<T>(this T item, params T[] list)
{
    return list.Contains(item);
}

...


if (!x.In(2,3,61,71))
...

You can rename it to IsElementOf if you prefer this name...

你可以把它重命名为IsElementOf如果你喜欢这个名字…

#2


5  

You could use following LinQ method:

您可以使用以下LinQ方法:

var list = new List<int> { 1, 2, 3, 4, 5 };
var number = 3;

if (list.Any(item => item == number))
    //number is in the list

And for the readability you can put it in an extension method:

对于可读性,你可以把它放在扩展方法中:

public static bool IsElementOf(this int n, IEnumerable<int> list)
{
    return list.Any(i => n == i);
}

//usage
if(3.IsElementOf(list)) //in the list

#3


5  

Old question, but haven't seen this simple answer:

老问题,但没见过这个简单的答案:

!new []{2, 3, 61, 71}.Contains(x)

#4


1  

what about

是什么

if(new[] { 2, 3, 61, 71 }.Except(x).FirstOrDefault() != 0)
{
   ...
}

or something on those lines?

或者是那些线条上的东西?

#5


0  

var list=CreateNewList(); //returns your list of elements
var element=GetElement(); //returns an element that might be in the list
if(list.Any(x=>x.Equals(element))
{
  //do something
}

It's still inverted from what you're used to but it's more expressive (if the list has any value that equals element).

它仍然与您所习惯的相反,但是它更有表现力(如果列表中有任何值等于元素)。

#6


0  

Assuming you meant && and not ||, you can just write a func and use that throughout your code. You can shorten the new[] part since the type (int) is infered by the in paramter of the func.

假设您的意思是&而不是||,那么您可以编写一个func并在整个代码中使用它。可以缩短新的[]部分,因为类型(int)是由func的in paramter推断的。

Func<int, bool> IsSafe = x => !new[] { 2, 3, 61, 71 }.Contains(x);

Console.WriteLine(IsSafe(68)); // is true
Console.WriteLine(IsSafe(2));  // is false