3

只是为了扔一些代码

string answer = "hotel"
if (answer == "house"|| answer == "hotel" || answer =="appartment")
{
    DoSomething()
}

我想知道是否有某种方法可以将其缩短为

string answer = "hotel"
if (answer == "house"|| "hotel" || "appartment")
{
    DoSomething()
}

我知道 switch 语句

switch (answer)
{
    case "house":
    case "hotel":
    case "appartment": DoSomething();
        break;
    default :DoNothing();
}

我只是想知道是否有像我上面描述的那样的语法糖。

4

10 回答 10

8

可以为此使用一些语法糖:

if((new[]{"house", "hotel", "apartment"}).Contains(answer))
{
}

请注意,这将动态创建一个新数组,因此可能比仅Boolean检查更昂贵。

于 2012-05-08T13:04:57.247 回答
7

您可以使用数组并使用包含。

所以在你的例子中:

string answer = "hotel";
string[] acceptable = new string[]{"house", "hotel", "appartment"};
if (acceptable.Contains(answer)){
    DoSomething();
}
于 2012-05-08T13:05:58.853 回答
4
public static bool Any<T>(T item, params T[] items)
{
    return items.Contains(item);
}

用法:

if (Any(6, 1, 2, 3, 4, 5, 6, 7))
{
    // 6 == 6
}

if (Any("Hi", "a", "cad", "asdf", "hi"))
{
}
else
{
    // "Hi" != "hi" and from anything else.
}

或者:

public string[] items = new string[] {"a", "cad", "asdf", "hi"};

...

if (Any("Hi", items))
{
    Works just as well.
}

您还可以进行更高级的比较。例如,如果您想要:

if (person.Name == p1.Name ||
    person.Name == p2.Name ||
    person.Name == p3.Name ||
    person.Name == p4.Name ||
    person.Name == p5.Name || ...)
{
}

你可以有:

public static bool Any<T>(T item, Func<T, T, bool> equalityChecker, params T[] items)
{
    return items.Any(x => equalityChecker(item, x));
}

并做:

if (Any(person, (per1, per2) => p1.Name == p2.Name, p1, p2, p3, p4, p5, ...)
{
}

编辑

如果你坚持,你当然可以做一个扩展方法:

public static bool Any<T>(this T item, params T[] items)
{
    return items.Contains(item);
}

用法:

var b = 6.Any(4, 5, 6, 7); // true

在签名中添加关键字“item”的相同逻辑适用于相等检查器的重载。

于 2012-05-08T13:09:09.383 回答
3

创建一个全局列表,然后用任何方法检查它。

List<string> validAnswers = new List<string> {"house", "house1", "apartment"};

if (validAnswers.Contains(answer))
    DoSomething();

在这种情况下,您List不会在每次answer检查时生成

于 2012-05-08T13:05:03.407 回答
2

我唯一能想到的就是使用列表或数组:

List<String> words = new List<String> { "house", "hotel", "appartment" };
String answer = "house";

if (words.Contains(answer))
{
   DoSomething();
}
于 2012-05-08T13:05:36.653 回答
1

你可以这样做

if(new string[]{"house","hotel","appartment"}.Contains(asnwer))
{
...
}

或者

if(new List<string>(){"house","hotel","appartment"}.Any(x=>x == answer)
{
}

也可以像扩展方法一样添加它,并使用...

于 2012-05-08T13:05:38.270 回答
1

考虑添加一个可以接受任何字符串的扩展方法......

string answer = "hotel"
if (answer.EqualsAny("house", "hotel", "appartment"))
{
    DoSomething()
}
// Extending the thought to another version
if (answer.EqualsAll("house", "hotel", "appartment"))
{
    DoSomething()
}

public static class Extensions
{
    public static bool EqualsAny(this string value, params string[] compareAgainst)
    {
        foreach (var element in compareAgainst)
        {
            if(value == element)
               return true;
        }
        return false;
    }
    public static bool EqualsAll(this string value, params string[] compareAgainst)
    {
        foreach (var element in compareAgainst)
        {
            if(value != element)
                return false;
        }
        return true;
    }
}
于 2012-05-08T13:12:27.570 回答
0

如果您的比较参数是固定的,您可以选择带标志的 ENUM。参考MSDN。然后你可以有想要的行为。你也可以参考这个帖子

于 2012-05-08T13:09:47.200 回答
0

在这种情况下,我通常使用 switch 语句,但正如在上面的几个答案中已经说过的,您可以将结果放入列表或数组中并以这种方式进行检查。

于 2012-05-08T13:15:37.717 回答
0

我会使用这样的扩展方法:

public static bool In<T>(this T item, params T[] items)
{
    return items.Contains(item);
}

像这样使用它:

if ("hotel".In("house", "hotel", "apartment"))
or
if (1.In(1,2))
于 2012-05-11T21:59:25.903 回答