Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Common data structures

1: Array, the ArrayList
1. Array is allocated in continuous memory and cannot be expanded at will. Data insertion is slow. 2. High Array performance, fast index search, no more data performance is affected. 3, ArrayList is variable length, does not limit the type, so may not be safe, put different types, may use the wrong. All elements are objects, and the value types are boxed.Copy the code
2: List, LinkedList
1, List is also an array, generic, variable length, no boxing unboxing type safety. 2, LinkedList add delete efficiency, slow query, do not support index because not together memory, to start from scratch.Copy the code
3 :Queue/Stack (the greatest and easiest to understand structure)
Queue first in first out (FIFO) 2. Stack first and then out.Copy the code
4: The Hashtable/Dictionary is not linear and has a hash structure
(2) Fast add, delete, alter, and query: space for time (3) thread-safe collection (1) Dictionary generic Hashtable (2) index access (3)Copy the code

Iterator pattern

1: implementation mode
You can use foreach only if you have an IEnumerator GetEnumerator() insideCopy the code
2: Features and principles
1. Implement the iterator pattern.Copy the code
Public interface IIterator<T> {/// <summary> // </summary> T Current {get; } /// <summary> // move to the next object, whether there is /// </summary> /// <returns></returns> bool MoveNext(); Reset / / / < summary > / / / / / / < summary > void Reset (); } public class MenuIterator: IIterator<Food> {private Food[] _FoodList = null; Public MenuIterator(KFCMenu KFCMenu) {this._foodlist = kfcmenu.getFoods (); } private int _CurrentIndex = -1; Public Food Current {get {return this._foodlist [_CurrentIndex]; }} public bool MoveNext() {return this._foodlist. Length > ++ this._currentIndex; Public void Reset() {this._currentIndex = -1; }} Public class KFCMenu {private Food[] _FoodList = new Food[3]; Public KFCMenu() {this._foodList [0] = new Food() {Id = 1, Name = "hamburger ", Price = 15}; This._foodlist [1] = new Food() {Id = 2, Name = "coke ", Price = 10}; This._foodlist [2] = new Food() {Id = 3, Name = "fries ", Price = 8}; } public Food[] GetFoods() { return this._FoodList; } public IIterator<Food> GetEnumerator() { return new MenuIterator(this); } // call KFCMenu KFCMenu = new KFCMenu(); IIterator<Food> foodIterator = kfcMenu.GetEnumerator(); while (foodIterator.MoveNext()) { Food food = foodIterator.Current; }Copy the code
2. The advantage of implementing the iterator pattern is uniform access.Copy the code
3 :Yield
public IEnumerable<int> Power() { for (int i = 0; i < 10; i++) { yield return this.Get(i); if (i == 3) yield break; }}Copy the code
1. Yield must appear in IEnumerable. Yield is a state machine for iterators that can be queried late and loaded on demand.Copy the code
4.conclusion
If a class can be foreach, it must implement an IEnumerable, or if it has the IEnumerator GetEnumerator() method and implements its own iterator, IIterator. When accessing a loop corresponding to an iterable data source, it searches for and returns data from an internal iteratorCopy the code

Dynamic keyword

Dynamic can be implicitly converted to any type at runtime, regardless of compiler checksCopy the code