C#中的Collection 2

Core Generic interface

 C#中的Collection 2_第1张图片   C#中的Collection 2_第2张图片

 

IEnumerable<T>:you can interate my elemnts, no need to know the count, can not modify my contents (Array在这里)

var myString = new string[] {"1","2","3","4"}; //可以迭代但是不可以修改Collection本身但是item的值可以修改

ICollection<T>:I know how many elements I have, and you can modify my contents, but I don't know what kind of collection (不经常用)

int ICollection<T>.Count

IList<T> (List在这里)

 

var myStringList = new List<string> {"1","2","3","4"}; //可以迭代可以修改Collection本身

IDictionary<TKey, TValue> (Dictionary在这里)

ISet<T> (Set在这里)

IEnumerable<T>

IEnumerable<T>类型可以迭代,具体实现要用Enumerator,但是foreach已经为我们做好了转换,而且foreach可以作用与所有的collection.

ICollection<T>

比IEnumberable<T>多了写

C#中的Collection 2_第3张图片Count只有ICollection有,Array没有Count property,但是有Array.Length,如果非要数Count就用Linq的IEnumerable<T>.Count()方法。

对于ICollection Linq有ICollection<T>.Count

IList<T>

因为继承自IEnumerable<T>所以有this[index]和indexof(),并且增加了Insert(), 和RemoveAt()

//look up element
string saturday = dayOfWeek[5];

//replace element
dayOfWeek[5] = "SlaveDay";

IDictionary<TKey, TValue>

You can look up my element with a Key

Country country =  countries["UK"];

C#中的Collection 2_第4张图片

 

 

 

 

 

 

你可能感兴趣的:(Collection)