C # Study Notes 11,

Source: Internet
Author: User

C # Study Notes 11,

1.List. binarySearch (): BinarySearch () uses a binary search algorithm, which requires that the elements have sorted in order. It is characterized by a negative integer if the elements are not found, bitwise inversion of this value (~) The result is an index greater than the next element of the element to be searched. If there is no larger value, it is the total number of elements. In this way, you can easily Insert new values at specific locations in the list, while maintaining the sorted status. You can view the CollecationData. TestBinarySearch () code. Note that, if the order is not arranged in advance, an element may not be found, that is, it is indeed in the list.

2.Value assignment of Dictionary <TKey, TValue>: There are two ways to assign values,

(1) Use the Add () method to Add a key-Value Pair element. However, if an identical key value is added, an exception is thrown.

(2) assign values using the indexer, for example, dictionary [key] = value. If this key value does not exist, add it. If this key value exists, overwrite it.

3.The dictionary class does not have a specific order. elements are saved to a hash using the hash code, which enables quick search. Therefore, if you use a foreach loop to traverse a dictionary class, the value is not accessed in a specific order.

4.Sorted collection classes: SortedDictionary <TKey, TValue> and SortedList <T>. SortedDictionary <TKey, TValue> elements are sorted by key, sortedList <T> elements are sorted by values. When you insert or delete elements in a sorted set, the execution time of the elements in the set is longer than that of the common set described earlier. You can view the CollecationData. TestSortedDictionary () code.

5.Stack collection Stack <T>: its elements are post-release. The three key methods are Push (), Pop (), Peek (),

(1) Push () sends elements to the set, and the elements do not have to be unique;

(2) Pop () obtains and deletes elements in the reverse order of addition;

(3) Peek () returns the next element that Pop () will obtain, but does not modify the elements in the stack.

6.Queue set <T>: The elements follow the first-in-first-out (the elements do not have to be unique), and Enqueue () is used to join the Queue and Dequeue () to leave the Queue (the element will be removed from the Queue ), it is equivalent to the two ends of a pipe. The queue set is automatically increased as needed. when data is no longer needed, we use the TrimToSize () method to restore the previous capacity.

7.Index OPERATOR: this [parameter] is used for declaration. It contains get and set. Index operators can obtain multiple parameters or even overload them. C # The template Code created by the compiler for the index operator is a special attribute index named Item. The default attribute name of the index in the template code is Item, but IndexerName can be used to mark the attribute) to specify a different name. Of course, there is no difference in actual use. It specifies a name for languages that do not directly support the indexer. The compiler can check this feature, but the IndexerName tag attribute itself does not appear in the pencil output, so it cannot be used through reflection.

8.How does the iterator (yield) work? C # When the compiler encounters an iterator, it expands the code into an appropriate pencil according to the enumeration number mode. In the generated code, C # The Compiler first creates a nested private class to implement the IEnumerator <T> interface, and its Current attribute and a MoveNext () method, the Current attribute returns a type corresponding to the type returned by the iterator.

9.Create multiple iterators for a single class (yield): Sometimes you may want different iteration sequences, such as reverse iterations and filtering results. To declare additional iterators in the class, you can encapsulate them in the attributes or methods that return IEnumerable <T> or IEnumrable.

10.Features of the yield statement: the yield return statement can be declared only when the IEnumerator <T> or IEnumerable <T> type is returned. More specifically, only in the GetEnumerator () method that returns IEnumerator <T>, or in the method that returns IEnumerable <T> but not GetEnumerator, to declare yield return.

Public class CollecationData {public static void TestBinarySearch () {List <string> list = new List <string> () {"public", "protected", "private "}; string item = "protected internal"; list. sort (); int index = list. binarySearch (item); if (index <0) {list. insert (~ Index, item);} list. forEach (Console. writeLine);} public static void TestSortedDictionary () {SortedDictionary <string, string> sortDict = new SortedDictionary <string, string> (); int index = 0; sortDict. add (index ++. toString (), "object"); sortDict. add (index ++. toString (), "byte"); sortDict. add (index ++. toString (), "uint"); sortDict. add (index ++. toString (), "ulong"); sortDict. add (index ++. toString (), "float"); sortDict. add (index ++. toString (), "char"); sortDict. add (index ++. toString (), "bool"); sortDict. add (index ++. toString (), "ushort"); sortDict. add (index ++. toString (), "decimal"); sortDict. add (index ++. toString (), "int"); sortDict. add (index ++. toString (), "sbyte"); sortDict. add (index ++. toString (), "short"); sortDict. add (index ++. toString (), "long"); sortDict. add (index ++. toString (), "void"); sortDict. add (index ++. toString (), "double"); sortDict. add (index ++. toString (), "string"); Console. writeLine ("key value Hashcode"); Console. writeLine ("--- ----- --------"); foreach (KeyValuePair <string, string> item in sortDict) {Console. writeLine ("{0,-5} {1,-9} {2}", item. key, item. value, item. key. getHashCode () ;}} public static void TestDict () {Dictionary <string, string> dict = new Dictionary <string, string> (); dict ["1"] = "1"; dict. add ("2", "II"); foreach (var item in dict) {Console. writeLine ("key = {0}, value = {1}", item. key, item. value );}}}

------------------- The above content is organized according to "C # the third edition of this topic ".

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.