2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > C#中怎么判断一个数组中是否存在某个数组值

C#中怎么判断一个数组中是否存在某个数组值

时间:2024-01-03 03:44:10

相关推荐

C#中怎么判断一个数组中是否存在某个数组值

(1) 第一种方法:

int[] ia = {1,2,3};

int id = Array.IndexOf(ia,1); // 这里的1就是你要查找的值

if(id==-1)

// 不存在

else

// 存在

(2) 第二种方法:

string[] strArr = {"a","b","c","d","e"};

bool exists = ((IList)strArr).Contains("a");

if(exists)

// 存在

else

// 不存在

注意: 用IList需要using System.Collections;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。