C#中的Collection 2

时间:2023-03-08 22:25:38
C#中的Collection 2

Core Generic interface

C#中的Collection 2   C#中的Collection 2

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

var myString = new string[] {"","","",""}; //可以迭代但是不可以修改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> {"","","",""}; //可以迭代可以修改Collection本身

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

ISet<T> (Set在这里)

IEnumerable<T>

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

ICollection<T>

比IEnumberable<T>多了写

C#中的Collection 2Count只有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[]; //replace element
dayOfWeek[] = "SlaveDay";

IDictionary<TKey, TValue>

You can look up my element with a Key

Country country =  countries["UK"];

C#中的Collection 2