只为成功找方向,不为失败找借口

每天都不能停止前进的脚步
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2010-06-22 15:56  冰碟  阅读(19694)  评论(0编辑  收藏  举报

(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;