If 数组语句
If Statement With Array
不是写多个 if()
语句,有没有办法像下面那样做?
string redlight = "Larry";
string[] names = { "Joe", "Jack", "Bob", "Larry", "Alpha", "Mick", "Lance", "Ben" };
if (redlight == names)
Console.WriteLine("It's in the array");
您可以使用 .Contains()
if (names.Contains(redlight))
Console.WriteLine("It's in the array");
else
Console.WriteLine("It's not in the array");
或.Any()
if (names.Any(x=>x==redlight))
Console.WriteLine("It's in the array");
else
Console.WriteLine("It's not in the array");
检查这个 Working example
不是写多个 if()
语句,有没有办法像下面那样做?
string redlight = "Larry";
string[] names = { "Joe", "Jack", "Bob", "Larry", "Alpha", "Mick", "Lance", "Ben" };
if (redlight == names)
Console.WriteLine("It's in the array");
您可以使用 .Contains()
if (names.Contains(redlight))
Console.WriteLine("It's in the array");
else
Console.WriteLine("It's not in the array");
或.Any()
if (names.Any(x=>x==redlight))
Console.WriteLine("It's in the array");
else
Console.WriteLine("It's not in the array");
检查这个 Working example