Collection定義
Collection是個關於一些變量的集合,按功能可分為Lists,Dictionaries,Sets三個大類。
- Lists:是一個有序的集合,支持index look up。
- Array
- List<T>
- Dictionaries:同TKey store 一些TValue集合,沒有特定order
- Sets:用來form collections into onther collection,比如HashSet<T>
Collection提供的一些公共方法
是Collection就至少會有Enumerat,但是不一定都有Look up(比如Sets就沒有,Statcks和Queues也是有自己的固定排列順序,不提供look up的方法),注意List的look up用index,而Dictionary的look up用Key。
For adding和removing的位置可能也不一樣,list可以指定加和減的位置,而Statck是先進先出
Array
1: 有Fixed size並且是固定從上到下的順序儲存,index從0開始。

1 static void Main(string[] args) 2 { 3 //Declare 4 string[] daysOfWeek ={ 5 "Monday", 6 "Tuesday", 7 "Wednesday", 8 "Thurday", 9 "Friday", 10 "Saturday", 11 "Sunday" 12 }; 13 Console.WriteLine("Type in index of day of look up>"); 14 int index = int.Parse(Console.ReadLine()); 15 16 //look up using index 17 Console.WriteLine(daysOfWeek[index]); 18 19 //Enumerates 20 foreach (string day in daysOfWeek) 21 { 22 Console.WriteLine(day); 23 } 24 25 //replace 26 daysOfWeek[5] = "PartyDay"; 27 }
- 由於Array是固定size,所以沒有Add和Remove item
2: Array是reference type,有兩種initialize
int[] x4 = new int[5]; //這個5不可以省 int[] x5 = new int[5] {1, 4, 9, 16, 25}; //可以全部寫出來
int[] x5 = new int[] {1, 4, 9, 16, 25}; //讓compiler來認有幾個元素
var x5 = {1, 4, 9, 16, 25}; //int[]可以換成var,后面賦值都可以都不寫int[]
int eight = 8; int[] square = new int[]{ 1, 2 * 2, eight + 1, int.Parse("16"), (int)Math.Sqrt(625) };
3: Enumerating
這里出現錯誤,foreach 的interation plceholder是只讀的reference type,for不是
比較上面array[i]會改變原array里的元素,下面則不會
4: For VS Foreach
For:Can replace an array element,must directly access it using the index not using foreach
Foreach: Can't replace element(只讀) but can modify value of elements,對其他readonly collection也一樣

1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Person[] student = new Person[] 6 { 7 new Person{Name = "Shawn", Age = 33}, 8 new Person{Name = "Yawei", Age = 30} 9 }; 10 //foreach的iteration placeholder是只讀不可以改 11 //我們這里沒有直接改這個reference 12 //指定的每個person object,比如指向別的object 13 //而是修改這個object里面的值 14 foreach (Person person in student) 15 { 16 person.Age = 20; 17 } 18 19 foreach (Person person in student) 20 { 21 Console.WriteLine(person); 22 } 23 Console.ReadLine(); 24 } 25 26 } 27 public class Person 28 { 29 public string Name{get; set;} 30 public int Age{get; set;} 31 public override string ToString() 32 { 33 return string.Format("{0} 's age = {1}", Name, Age); 34 } 35 }
在collection里包括array == 是判斷reference是否相等的,而不是值。
Derived reference can used in place of base reference(this can apply to all collections)

static void Main(string[] args) { Human[] human = new Human[] { new Human{Name = "Shawn", Age = 33}, new Human{Name = "Yawei", Age = 30} }; human[0] = new Employee { Name = "Tutu", Age=12, Salary=120.2f}; } public class Employee : Human { public float Salary { get; set; } } public class Human{ public string Name { get; set; } public int Age { get; set; } }
5: Convariance

static void Main(string[] args) { object[] obj1 = new object[3]; string[] daysOfWeeks = { "Monday", "Tuesday", "Wendesday" }; //this can work obj1[0] = 3; //這個是reference type,obj2 point to daysOfWeeks's location object[] obj2 = daysOfWeeks; //這句有問題,因為refer的地址是一個string[] type //compile time的時候VS發現不了問題,會直接給runtime 帶來break的后果 //same result as daysOfWeeks[0] = 3; obj2[0] = 3; daysOfWeeks[0] = 3; }
解釋:
一般Convariance不允許用在Collection(List<T>, Dictionial<TKey, TValue>)上即Collection of derived type to a collection of base type,即使像上面用在了array上也會有或許被broke掉的危險。
但是enumerator,enumerable可以做convariance,因為他們是只讀的type保證是安全的,不會修改collection。
var strList = new List<string>{"Monday","Tuesday"}; List<object> objList = (List<Object>)strList; //compiler will show error, can not do this kind of covariance IEnumerable<object> objEnum = strList; //this can do
6: Array的方法
一般有普通Array方法或者LINQ方法,Array方法tend to work in-place,而LINQ總是tend to return new object
Copy:
- Array.CopyTo(普通)
- ToArray(LINQ)
Reverse:
- Array.Reverse(普通,static方法)
- var reversed = daysOfWeek.Reverse(); (LINQ方法)
注意LINQ方法經常return IEnumerable 而不是Array,如果需要變為Array要在后面加一個.ToArray
Sort:
Array.Sort
Array.Sort([T], comparer)
IEnumerable<T>.OrderBy();
Binary Search:
Array.BinarySearch([T], "Sunday")
Search
Find item in array: Array.IndexOf(), Array.LastIndexOf()
Find item with condition in array: Array.FindIndex([T], x=> x[0] == 'W'), Array.FindAll([T], x=> x.length == 6)