说出来:
if (stars == 2 || stars ==6 || stars ==10)
{
do something
}
是否有办法将它们组合在一起,就好像:
if (stars == {2, 4, 6}) <--- MATLAB style
{
do something
}
答案 0 :(得分:12)
public static class GenericExtensions
{
public static bool In<T>(this T @this, params T[] listOfItems)
{
if (null == listOfItems) return false;
return listOfItems.Contains(@this);
}
}
然后像:
一样使用它 if (2.In(1,2,3,4))
答案 1 :(得分:4)