Basic operations such as querying and deleting data are the basis of any programming language. Therefore, I studied the more commonly used types of data operations in C# and took notes.
List Query When processing relatively large data, use the HashSet
HashSet
List search complexity O(n), HashSet search complexity O (1)
Delete and add operations of the Dictionary class:
By default, when not sorted, the position of the added element is at the position of the deleted element.
If sorted, the position of the added element will still be the position of the element before unsorting .
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("未经排序:");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("未经处理:");foreach (var k in dic_sort) { Console.WriteLine(k.Key + " " + k.Value); } Console.WriteLine("经过删除添加处理:"); _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...
The above is the detailed content of C# Basic Operation Optimization Example Tutorial. For more information, please follow other related articles on the PHP Chinese website!