C # Optimization of basic operations based on notes,
Basic operations such as data query and deletion are the foundation of any programming language. Therefore, I have studied the commonly used data operation types in C # And take a note.
During List query, HashSet <T> is used to process relatively large data, because List is operated based on linear tables. however, BinarySearch is embedded with binary search. Therefore, you can sort binary search after storage. however, you can also design it as follows: Dictionary <TKey, List <T> uses the efficient search and query capability of Dictionary to search for List <T> objects. however, the data is stored using List <T>.
HashSet <T> is a collection class that does not contain repeated types. this set is quickly operated based on the hash value. compared with HashTable <TKey, TValue>, this collection class only contains one type parameter, not stored based on key-value pairs to find elements. to determine whether an element exists, you only need to call the Contains () method.
List Search complexity O (n), HashSet search complexity O (1)
Delete and add a Dictionary class:
By default, elements are deleted when they are not sorted.
If it is sorted, the position of the added element will still be the element position before unsorted.
Static void Main (string [] args) {Dictionary <int, int> _ dic = new Dictionary <int, int> (); _ dic. add (3, 3); _ dic. add (1, 1); _ dic. add (2, 2); _ dic. add (6, 6); Console. writeLine ("unordered:"); foreach (var k in _ dic) {Console. writeLine (k. key + "" + k. value);} var dic_sort = from dic in _ dic orderby dic. key select dic; Console. writeLine ("unprocessed:"); foreach (var k in dic_sort) {Console. writeLine (k. key + "" + k. value);} Console. writeLine ("added after deletion:"); _ dic. remove (2); _ dic. add (4, 4); foreach (var k in _ dic) {Console. writeLine (k. key + "" + k. value);} Console. read ();}
You can also test it yourself...