Generic【Pluralsight】
prepare Employee Structure
namespace CollectIt { public class Employee { public string Name { get; set; } public int DepartmentId { get; set; } } }
Array: T[]
- 定义
fix size, must define how many instance at beginning, no add methord
Employee[] employees = new Employee[]{ new Employee{Name = "Shawn", Address="Jurong West"}, new Employee{Name = "Rebeeca", Address="Bukit Timah"} }; foreach (var employee in employees) { Console.WriteLine("{0} live in {1}", employee.Name, employee.Address); } for (int i = 0; i < employees.Length; i++) { Console.WriteLine(employees[i].Name); }
可以foreach,可以[i],长度是Length
List[T]
- 定义
自动增加Capacity(使用Add),可以[i],长度使用Count
List<Employee> employees = new List<Employee>{ new Employee{Name = "Shawn", Address="Jurong West"}, new Employee{Name = "Rebeeca", Address="Bukit Timah"} }; employees.Add(new Employee { Name = "Chris", Address = "Yi Shun" }); foreach (var employee in employees) { Console.WriteLine("{0} live in {1}", employee.Name, employee.Address); } for (int i = 0; i < employees.Count; i++) { Console.WriteLine(employees[i].Name); }
当增加项目到List[T]若超出capacity,增加一倍。
var numbers = new List<int>(); var capacity = -1; while (true) { if (numbers.Capacity != capacity) { capacity = numbers.Capacity; Console.WriteLine(capacity); } numbers.Add(1); //无线循环,每次增加超过之前的capacity的时候list会增加一倍capacity }
- 方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Linq; namespace CollectIt.Tests { [TestClass] public class ListTests { [TestMethod] public void A_List_Can_Insert() { List<int> integers = new List<int> { 1, 2, 3 }; integers.Insert(1, 6); //在index=1的位置增加一个值6 Assert.AreEqual(6, integers[1]); } [TestMethod] public void A_List_Can_Remove() { List<int> integers = new List<int> { 1, 2, 3 }; integers.Remove(2); //remove值2 Assert.IsTrue(integers.SequenceEqual(new [] { 1,3 })); } [TestMethod] public void A_List_Can_Find_Things() { List<int> integers = new List<int> { 1, 2, 3 }; Assert.AreEqual(integers.IndexOf(3), 2); //找到值为3的所有index } } }
Queue[T]
- 定义
需要遵循一定order加入和拿出,Queue遵循FIFO
Queue<Employee> queue = new Queue<Employee>(); queue.Enqueue(new Employee { Name = "Alex", Address = "1st street" }); queue.Enqueue(new Employee { Name = "Bob", Address = "city link street" }); queue.Enqueue(new Employee { Name = "Filea", Address = "gimh street" }); while (queue.Count > 0) { var employee = queue.Dequeue(); Console.WriteLine(employee.Name); }
//输出:
//Alex
//Bob
//Filea
不能random的pickitem,没有[1]
- 方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; namespace CollectIt.Tests { [TestClass] public class QueueTests { [TestMethod] public void Can_Peek_At_Next_Item() { var queue = new Queue<int>(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); Assert.AreEqual(1, queue.Peek()); //相类似Dequeue,只是Peek第一个进去的,不拿出 } [TestMethod] public void Can_Search_With_Contains() { var queue = new Queue<int>(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); Assert.IsTrue(queue.Contains(2)); //查找是否有值2? } [TestMethod] public void Can_Convert_Queue_To_Array() { var queue = new Queue<int>(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); var asArray = queue.ToArray(); queue.Dequeue(); Assert.AreEqual(1, asArray[0]); //如果需要找出所有存储在queue的话,可以把其转换为array Assert.AreEqual(2, queue.Count); } } }
Stack[T]
- 定义
需要遵循一定order加入和拿出,Stack遵循LIFO
Stack<Employee> stack = new Stack<Employee>(); stack.Push(new Employee { Name = "Alex", Address = "1st street" }); stack.Push(new Employee { Name = "Bob", Address = "city link street" }); stack.Push(new Employee { Name = "Filea", Address = "gimh street" }); while (stack.Count > 0) { var employee = stack.Pop(); Console.WriteLine(employee.Name); }
No random access: Stack[0], no Stack.Add, no Stack.Insert
- 方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; namespace CollectIt.Tests { [TestClass] public class StackTests { [TestMethod] public void Can_Peek_At_Next_Item() { var stack = new Stack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); Assert.AreEqual(3, stack.Peek()); } [TestMethod] public void Can_Search_With_Contains() { var stack = new Stack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); Assert.IsTrue(stack.Contains(2)); //看看stack是否有值2 } [TestMethod] public void Can_Convert_To_Array() { var stack = new Stack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); var asArray = stack.ToArray(); stack.Pop(); Assert.AreEqual(3, asArray[0]); Assert.AreEqual(2, stack.Count); } } }