首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#中如何判断一个数组中是否存在某个数组值 转

2012-10-14 
C#中怎么判断一个数组中是否存在某个数组值 转(1) 第一种方法:int[] ia {1,2,3}int id Array.IndexOf

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;

热点排行