gpt4 book ai didi

c# - 如何在 if 语句中组合多个 OR 语句

转载 作者:行者123 更新时间:2023-11-30 13:14:36 25 4
gpt4 key购买 nike

这样说:

if (stars == 2 || stars ==6 || stars ==10)
{
do something
}

有没有办法将它们组合在一起,就像:

if (stars == {2, 4, 6}) <--- MATLAB style
{
do something
}

最佳答案

你可以这样写一个扩展:

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))

关于c# - 如何在 if 语句中组合多个 OR 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40609145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com