C#學習筆記11

來源:互聯網
上載者:User

標籤:賦值   ack   byte   散列   執行時間   二分搜尋   ash   ++   特定   

1.List.BinarySearch():BinarySearch()採用的是二分搜尋演算法,要求元素已經排好序,其特點是假如元素沒有找到,會返回一個負整數,該值的按位取反(~)結果是“大於被尋找元素的下一個元素”的索引,如果沒有更大的值,則是元素的總數。這樣一來就可以在列表中的特定位置方便地插入新值,同時保持已排序的狀態。可查看CollecationData.TestBinarySearch()代碼。要注意的是,假如事先沒有排好序,那麼不一定能找到一個元素,即是它確實在列表中。

2.Dictionary<TKey,TValue>的賦值:其賦值有2種方式,

(1)使用Add()方法添加索引值對元素,但是若添加了一個相同的索引值,會引發一個異常。

(2)使用索引器賦值,如dictionary[key]=value,若沒有該索引值則進行添加,若有該索引值則進行覆蓋。

3.字典類是沒有特定的順序,元素使用散列碼存到一個散列表中,這樣可以實現快速檢索。所以如果使用foreach迴圈來遍曆一個字典類,將不按照特定的順序來訪問值。

4.已排序的集合類:SortedDictionary<TKey,TValue>和SortedList<T>,對SortedDictionary<TKey,TValue>元素是按照鍵排序的,對SortedList<T>元素是按照值排序的。在一個已排序的集合中插入或刪除元素時,由於要保持集合中的元素順序,所以相對前面描述的普通集合,執行時間要稍長一些。可查看CollecationData.TestSortedDictionary()代碼。

5.棧集合類Stack<T>:其元素是先進後出,三個關鍵的方法是Push()、Pop()、Peek(),

(1)Push()將元素送入集合,元素不必是唯一的;

(2)Pop()按照與添加時相反的順序擷取並刪除元素;

(3)Peek()返回Pop()將擷取的下一個元素,但不修改棧中元素。

6.列隊集合Queue<T>:其元素遵循先入先出(元素不必是唯一的),使用Enqueue()進行入隊與Dequeue()進行出隊(出隊會移除元素),相當於一個管子的兩端。列隊集合根據需要自動增大,當不再需要資料的時候,我們使用TrimToSize()方法來恢複以前的容量。

7.索引運算子:使用this[參數]進行聲明,內中含有get與set,索引運算子可以擷取多個參數,甚至可以重載。C#編譯器為索引運算子建立的CIL代碼是一個名為Item的特殊屬性索引器在CIL代碼中的屬性名稱預設為Item,但是可以使用IndexerName(標記屬性)來指定一個不同的名稱,當然在實際使用中並無區別,它是它為不直接支援索引器的語言指定了名稱。編譯器能檢查到這個特性,但是IndexerName標記屬性本身是不會在CIL輸出中出現的,所以不能通過反射來使用它。

8.迭代器(yield)是如何工作的:C#編譯器遇到一個迭代器時,會根據枚舉數模式將代碼展開成恰當的CIL,在產生的程式碼中,C#編譯器首先建立一個嵌套的private類來實現IEnumerator<T>介面,以及它的Current屬性和一個MoveNext()方法,Current屬性返回與迭代器的傳回型別對應的一個類型。

9.單個類建立多個迭代器(yield):有時候可能希望不同的迭代順序、比如逆向迭代、對結果進行篩選等,為了在類中聲明額外的迭代器,你可以把它們封裝到返回IEnumerable<T>或IEnumrable的屬性或方法中。

10.yield語句的特徵:只有在返回IEnumerator<T>或者IEnumerable<T>類型的成員中,才能聲明yield return 語句。更具體地說,只有在返回IEnumerator<T>的GetEnumerator()方法中,或者在返回IEnumerable<T>但不叫做GetEnumerator()的方法中,才能聲明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"] = "壹";        dict.Add("2", "貳");        foreach (var item in dict)        {            Console.WriteLine("key = {0} , value = {1}",item.Key,item.Value);        }    }}

-------------------以上內容根據《C#本質論 第三版》進行整理

C#學習筆記11

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.