C# 将变量与多个值进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2356949/
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-07 01:43:26  来源:igfitidea点击:

Comparing a variable to multiple values

c#

提问by Mongus Pong

Quite often in my code I need to compare a variable to several values :

在我的代码中,我经常需要将一个变量与多个值进行比较:

if ( type == BillType.Bill || type == BillType.Payment || type == BillType.Receipt )
{
  // Do stuff
}

I keep on thinking I can do :

我一直认为我可以做到:

if ( type in ( BillType.Bill, BillType.Payment, BillType.Receipt ) )
{
   // Do stuff
}

But of course thats SQL that allows this.

但当然这就是允许这样做的 SQL。

Is there a tidier way in C#?

C# 中有更整洁的方法吗?

采纳答案by Mark

You could do with with .Contains like this:

你可以用 .Contains 做这样的事情:

if(new[]{BillType.Receipt,BillType.Bill,BillType.Payment}.Contains(type)){}

Or, create your own extension method that does it with a more readable syntax

或者,创建您自己的扩展方法,使用更易读的语法来完成它

public static class MyExtensions
{
    public static bool IsIn<T>(this T @this, params T[] possibles)
    {
        return possibles.Contains(@this);
    }
}

Then call it by:

然后通过以下方式调用它:

if(type.IsIn(BillType.Receipt,BillType.Bill,BillType.Payment)){}

回答by Sergiy Belozorov

Try using C# HashSet for list of values. This can be especially useful if you need to compare many values to single set of values.

尝试使用 C# HashSet 获取值列表。如果您需要将多个值与一组值进行比较,这将特别有用。

回答by Yuriy Faktorovich

Assuming type is an enumeration, you could use the FlagsAttribute:

假设 type 是枚举,您可以使用FlagsAttribute

[Flags]
enum BillType
{
    None = 0,
    Bill = 2,
    Payment = 4,
    Receipt = 8
}

if ((type & (BillType.Bill | BillType.Payment | BillType.Receipt)) != 0)
{
    //do stuff
}

回答by Jarrett Meyer

Try checking out the Strategy Design Pattern (a.k.a. the Policy Design Pattern).

尝试查看策略设计模式(又名策略设计模式)。

public interface IBillTypePolicy
{
    public BillType { get; }
    void HandleBillType();
}
public class BillPolicy : IBillTypePolicy
{
    public BillType BillType { get { return BillType.Bill; } }

    public void HandleBillType() 
    { 
        // your code here...
    }
}

Here's a great post on how to dynamically resolve the policyfrom Los Techies.

这是一篇关于如何动态解决Los Techies策略好文章

回答by wasatz

There's also the switch statement

还有 switch 语句

switch(type) {
    case BillType.Bill:
    case BillType.Payment:
    case BillType.Receipt:
        // Do stuff
        break;
}

回答by bleeeah

Try using a switch

尝试使用开关

 switch (type)
    {
        case BillType.Bill:
        case BillType.Payment:

        break;
    }

回答by p.marino

What about getting an array of all Enums values and iterate through this?

获取所有枚举值的数组并遍历它怎么样?

http://maniish.wordpress.com/2007/09/27/iterate-through-enumeration-c/

http://maniish.wordpress.com/2007/09/27/iterate-through-enumeration-c/