C# 确定变量是否等于值“列表”中的值的最简洁方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:00:26  来源:igfitidea点击:

Most succinct way to determine if a variable equals a value from a 'list' of values

提问by Dan Herbert

If I have a variable in C# that needs to be checked to determine if it is equal to one of a set of variables, what is the best way to do this?

如果我在 C# 中有一个变量需要检查以确定它是否等于一组变量中的一个,那么最好的方法是什么?

I'm not looking for a solution that stores the set in an array. I'm more curious to see if there is a solution that uses boolean logic in some way to get the answer.

我不是在寻找将集合存储在数组中的解决方案。我更想知道是否有解决方案以某种方式使用布尔逻辑来获得答案。

I know I could do something like this:

我知道我可以做这样的事情:

int baseCase = 5;
bool testResult = baseCase == 3 || baseCase == 7 || baseCase == 12 || baseCase == 5;

I'm curious to see if I could do something more like this:

我很想知道我是否可以做更多这样的事情:

int baseCase = 5;
bool testResult = baseCase == (3 | 7 | 12 | 5);

Obviously the above won't work, but I'm interested in seeing if there is something more succinct than my first example, which has to repeat the same variable over and over again for each test value.

显然上面的方法行不通,但我有兴趣看看是否有比我的第一个例子更简洁的东西,它必须为每个测试值一遍又一遍地重复相同的变量。

UPDATE:
I decided to accept CoreyN's answer as it seems like the most simple approach. It's practical, and still simple for a novice to understand, I think.

更新:
我决定接受 CoreyN 的回答,因为这似乎是最简单的方法。我认为它很实用,而且对于新手来说仍然很容易理解。

Unfortunately where I work our system uses the .NET 2.0 framework and there's no chance of upgrading any time soon. Are there any other solutions out there that don't rely on the .NET 3.5 framework, besides the most obvious one I can think of:

不幸的是,在我工作的地方,我们的系统使用 .NET 2.0 框架,短期内不可能升级。除了我能想到的最明显的解决方案之外,是否还有其他不依赖于 .NET 3.5 框架的解决方案:

new List<int>(new int[] { 3, 6, 7, 1 }).Contains(5);

采纳答案by Corey

        bool b = new int[] { 3,7,12,5 }.Contains(5);

回答by Jon Galloway

I usually use CoreyN's solutionfor simple cases like that. Anything more complex, use a LINQ query.

对于这样的简单情况,我通常使用CoreyN解决方案。任何更复杂的,使用 LINQ 查询。

回答by Craig Tyler

Since you did not specify what type of data you have as input I'm going to assume you can partition your input into powers of 2 -> 2,4,8,16... This will allow you to use the bits to determine if your test value is one of the bits in the input.

由于您没有指定输入的数据类型,我假设您可以将输入划分为 2 的幂 -> 2,4,8,16 ......这将允许您使用这些位来确定如果您的测试值是输入中的位之一。

4 => 0000100
16 => 0010000
64 => 1000000

4 => 0000100
16 => 0010000
64 => 1000000

using some binary math...

使用一些二进制数学...

testList = 4 + 16 + 64 => 1010100
testValue = 16
testResult = testList & testValue

testList = 4 + 16 + 64 => 1010100
testValue = 16
testResult = testList & testValue

回答by Joe

You can do something similar with .NET 2.0, by taking advantage of the fact that an array of T implements IList<T>, and IList<T> has a Contains method. Therefore the following is equivalent to Corey's .NET 3.5 solution, though obviously less clear:

通过利用 T 数组实现 IList<T> 和 IList<T> 具有 Contains 方法这一事实,您可以使用 .NET 2.0 执行类似的操作。因此,以下等效于 Corey 的 .NET 3.5 解决方案,但显然不太清楚:

bool b = ((IList<int>)new int[] { 3, 7, 12, 5 }).Contains(5); 

I often use IList<T> for array declarations, or at least for passing one-dimensional array arguments. It means you can use IList properties such as Count, and switch from an array to a list easily. E.g.

我经常将 IList<T> 用于数组声明,或者至少用于传递一维数组参数。这意味着您可以使用 IList 属性(例如 Count),并轻松地从数组切换到列表。例如

private readonly IList<int> someIntegers = new int[] { 1,2,3,4,5 };