C#程序检查值是否在数组中
使用Array.Exists方法检查值是否在数组中。
设置一个字符串数组-
string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" };
假设您需要在数组中找到值“ keyboard”。为此,请使用Array.Exists()-
Array.Exists(strArray, ele => ele == "keyboard");
如果元素存在,则返回一个真值,如下所示:
示例
using System;using System.Text;
public class Demo {
public static void Main() {
string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" };
bool res1 = Array.Exists(strArray, ele => ele == "harddisk");
Console.WriteLine(res1);
bool res2 = Array.Exists(strArray, ele => ele == "keyboard");
Console.WriteLine(res2);
}
}
输出结果
FalseTrue
以上是 C#程序检查值是否在数组中 的全部内容, 来源链接: utcz.com/z/348969.html