C # Optimization of basic operations based on notes,

Source: Internet
Author: User

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...

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.