现在的位置: 首页 > 综合 > 正文

泛型集合(.NET 2.0)

2012年08月16日 ⁄ 综合 ⁄ 共 627字 ⁄ 字号 评论关闭

1、List<T>

  来判断是否为空:count属性;

  判断某个元素是否在该List中:List. Contains(T item);

  遍历可使用:foreach、for;

  List清空:List. Clear () ;

2、Dictionary<T1,T2>

  来判断是否为空:count属性;

  遍历可使用:

//KeyValuePair<T,K>
foreach (KeyValuePair<string, int> kv in list)
{
  Console.WriteLine(kv.Key + kv.Value);
} 
//3.0以上版本
foreach (var item in list) 
{
  Console.WriteLine(item.Key + item.Value);
} 
 //过键的集合取
foreach (string key in list.Keys)
{
  Console.WriteLine(key + list[key]);
} 
 //直接取值
foreach (int val in list.Values) 
{ 
  Console.WriteLine(val);
} 
//非要采用for的方法也可
List<string> test = new List<string>(list.Keys);
for (int i = 0; i < list.Count; i++)
{ 
  Console.WriteLine(test[i] + list[test[i]]);
}

抱歉!评论已关闭.