C#。怎么做,如果(A ==(B或C或D))。是否可以? [英] C#. Do if( a == (b or c or d)). Is it possible?

查看:923
本文介绍了C#。怎么做,如果(A ==(B或C或D))。是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有另一种方式来写是这样的:

Is there another way to write something like this:

if (a == x || a == y || a == z)

,我发现这样做是这样的一种方式:

One way that I found is doing it like this:

if( new [] {x,y,z}.Contains(a))

有没有其他的好办法呢?

Are there other good ways?

推荐答案

我常使用扩展方法,模仿SQL后

I often use an extension method that mimics SQLs IN:

public static bool IsIn<T>(this T obj, params T[] collection) {
   return collection.Contains(obj);
}



这样我可以做到

That way I can do

if(a.IsIn(b, c, d)) { ... }

这篇关于C#。怎么做,如果(A ==(B或C或D))。是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆