所谓,程序=数据结构+算法。

我目前的日常工作就是繁琐的业务流程和增删改查之类的。

其实繁琐的业务流程也不过是改变一下数据的状态。怪不得叫,面向数据库编程。哈哈。

所以呢,了解一下各种 .net内置的集合类对我来说是有很大帮助的。

这篇大概讲一下集合类的接口。

在 System.Collections, 命名空间下的是关于集合的接口以及系统集合类的一些定义

 public interface ICollection:IEnumerable
public interface IComparer
public interface IDictionary:ICollection,IEnumerable
public interface IDictionaryEnumerator:IEnumerato
public interface IEnumerable
public interface IEnumerator
public interface IEqualityComparer
public interface IHashCodeProvider
public interface IList:ICollection,IEnumerable
internal class KeyValuePairs
 //枚举接口
public interface IEnumerator
{
bool MoveNext();
object Current{get;}
void Reset();
}

这个方法接口里有两个方法,分别是移动到下一个项和复位初使。
还有一个属性,这个属性的返回值是object。
这个移动是单向的,只能一个方向移动,不可以自己选移动的方向和条目。

这个可以认为是集合类必须实现的基本接口。

//返回枚举接口
public interface IEnumerable
{
IEnumerator GetEnumerator();
}

在这两个接口的基础上形成其他的接口。

 //有序集合
public interface ICollection : IEnumerable
{
//把集合对象拷贝
void CopyTo(Arrayarray,intindex);
//集合的元素数目
intCount{get;}
//返回集合是否是多线程同步访问
boolIsSynchronized{get;}
//返回同步对象
objectSyncRoot{get;}
}
 //索引集合
public interface IList : ICollection,IEnumerable
{
int Add(object value);
void Clear();
bool Contains(object value);
int IndexOf(object value);
void Insert(int index,object value);
void Remove(object value);
void RemoveAt(int index); bool IsFixedSize{get;}
bool IsReadOnly{get;}
object this[intindex]{get;set;}
}

这个接口就很丰富了,有增加成员,删除成员等一些方法。

 //字典集合或键值集合
public interface IDictionary : ICollection,IEnumerable
{
void Add(objectkey,objectvalue);
void Clear();
bool Contains(object key);
IDictionaryEnumerator GetEnumerator();
void Remove(object key); bool IsFixedSize{get;}
bool IsReadOnly{get;}
object this[object key]{get;set;}
ICollection Keys{get;}
ICollection Values{get;}
}

集合类型都实现了 IEnumerable(返回枚举接口) 接口,从而可以使用 foreach 迭代循环。
实现了 ICollection(有序集合) 接口的集合类表明集合中的元素是有先后顺序的。
IList 接口继承了 ICollection(有序集合) 接口,实现了 IList 接口的集合类不止表明集合中的元素是有先后顺序,而且表明集合类可以通过下标访问的方式来访问集合元素。
IDictionary 接口也继承了 ICollection(有序集合) 接口,实现了 IDicionary 接口的集合类可以通过下标 key 访问的访问方式访问集合元素。

ICollection 接口扩展了 IEnumerable;
IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。
IDictionary 实现是键/值对的集合,如Hashtable类。
IList 实现是值的集合,其成员可通过索引访问,如 ArrayList 类。

某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现ICollection接口。
如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。

在此 ILIST 接口的基础上产生的一些类型 是我们实际编程中能用的到的,比如

 //数组类这个类是抽象的
public abstract class Array : ICloneable, IList, ICollection, IEnumerable

我们平时用到的比如 int[] intarray = new int[] {} 是派生于这个抽象类。

但是数组是从 Array 隐式派生的,这是由编译器完成的,所以我们看不到具体的实现过程。

比如

 Array array1;
int[]a=newint[]{};
string[]b=newstring[]{};
array1=a;
array1=b

因为数组的大小是固定的,类型也是固定的。就发明了 ArrayList,它是一个长度可变的object数组。

 public class ArrayList : IList, ICollection, IEnumerable, ICloneable

object 可以传入任何类型但是涉及装箱和拆箱的操作,所以性能受影响。
Stack和Queue栈和队列是有序集合。
HashTable 实现了 IDictionary 接口。是键值对的集合。
也是键和值都是object但是索引,集合下标是int,value是object
但是hashtable键值不是按照插入的顺序进行排列。

 public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable

SortList 也是字典类,但是按照顺序排放.
非范性的集合类就这些了。

泛型集合类

System.Collections.Generic 是.NET2.0新增的一个命名空间。

C#1.0 中的集合类型都存在着装箱,拆箱的性能问题以及潜在的类型安全问题。

丑陋的设计必须得到改进,于是 C#2.0 就引入了泛型这个概念。

泛型即解决了装箱拆箱的性能问题,又解决了潜在的类型转换安全问题,所以在实际应用中,推荐使用泛型集合代替非泛型集合。(泛型实际是编译器帮我们生成了代码)

 //这个泛型接口继承自非泛型的同名接口
public interface IEnumerator<T> : IDisposable , IEnumerator
{
bool MoveNext();
T Current { get; }
void Reset();
}
 //返回枚举 泛型的同名接口
public interface IEnumerable<T> : IEnumerable//
{
IEnumerator<T> GetEnumerator();
}

IEnumerable 只有一个方法 GetEnumerator() 即得到遍历器。

 //有序集合
public interface ICollection<T> : IEnumerable<T>,IEnumerable
{
void Add(Titem);
void Clear();
bool Contains(Titem);
void CopyTo(T[]array,intarrayIndex);
bool Remove(Titem); int Count{get;}
bool IsReadOnly{get;}
}

ICollection 与 ICollection<T> 略有不同,

ICollection 不提供编辑集合的功能,即 Add() 和 Remove()。包括检查元素是否存在的 Contains 也不支持。

 public interface IDictionary<TKey,TValue> : ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey,TValue>>,IEnumerable
{
void Add(TKeykey,TValuevalue);
bool ContainsKey(TKeykey);
bool Remove(TKeykey);
bool TryGetValue(TKeykey,outTValuevalue); TValue this[TKeykey]{get;set;}
ICollection<TKey> Keys{get;}
ICollection<TValue> Values{get;}
}
 public interface IList<T> : ICollection<T>,IEnumerable<T>,IEnumerable
{
int IndexOf(Titem);
void Insert(intindex,Titem);
void RemoveAt(intindex); T this[intindex]{get;set;}
}

IList 是直接继承自 ICollection 和 IEnumerable。
所以它包括两者的功能,并且支持根据下标访问和添加元素。IndexOf, Insert, RemoveAt等等。
可以说,IEnumerable 支持的功能最少,只有遍历。而 ICollection 支持的功能稍微多一点,不仅有遍历还有维护这个集合的功能。而 IList 是最全的版本。

 public class List<T> : IList<T>,ICollection<T>,IEnumerable<T>,IList,ICollection,IEnumerable
public class Dictionary<TKey,TValue>:IDictionary<TKey,TValue>,ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey,TValue>>,IDictionary,ICollection,IEnumerable

泛型 list 和泛型 Dictionary 都是继承自泛型 ICollection

IReadOnlyList<T>
这个是在 Framework4.5 中新增的接口类型。可以被看作是IList<T>的缩减版,去掉了所有可能更改这个集合的功能。比如:Add, RemoveAt 等等。

泛型集合与非泛型集合的对应

ArrayList    =>  List<T>
Stack,Queue  =>  Stack<T>,Queue<T>  
HashTable           =>  Dictionary<TKey,TValue>
SortedList            =>  SortedList<TKey,TValue> / SortedDictionary<TKey,TValue>

参考:http://www.cnblogs.com/C-CHERS/p/5809087.html

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(10-1)译-&gt;非Code Frist方式返回一个实体集合
  2. Android WebView 保存cache至External Storage
  3. __cdecl和__stdcall
  4. ORA-1461 encountered when generating server alert SMG-3500
  5. webstorm无法格式化
  6. hadoop作业调优参数整理及原理【转】
  7. css的!important规则对性能有影响吗
  8. input里面check 状态检测
  9. 无缝滚动 jQuery经典代码 (收藏)
  10. PHP 9: 表达式
  11. Debian下的&#39;aptitude update&#39;失败处理
  12. 海量数据挖掘MMDS week7: 局部敏感哈希LSH(进阶)
  13. windows安装weblogic和域的建立
  14. 领域驱动设计和Spring
  15. Eureka-Client(Golang实现)
  16. Redis操作hash
  17. flask_admin 笔记七 扩展功能
  18. 目标(web前端)
  19. 仿iPhone滑屏操作
  20. python学习之itsdangerous模块

热门文章

  1. Django认证系统实现的web页面
  2. ORB_SLAM2 源码阅读 ORB_SLAM2::Initializer
  3. 2016.07.15——istringstream测试
  4. reshape中的-1
  5. Prepare tasks for django project deployment.md
  6. Unity3D Instantiate慢的问题
  7. Python开发环境(1):Eclipse+PyDev插件
  8. kafka集群及监控部署
  9. C++ : Boost : Rational 有理数类
  10. RESTful API 和 Django REST framework