C# 泛型集合實例應用淺析

黄舟
發布: 2016-12-21 14:41:22
原創
1132 人瀏覽過

C# 泛型集合了解之前我們明白集合是OOP中的重要概念,C#中對集合的全面支持更是該語言的精華之一。 C# 泛型是C# 2.0中的新增元素(C++中稱為模板),主要用於解決一系列類似的問題。這種機制允許將類別名稱作為參數傳遞給泛型類型,並產生相應的物件。將泛型(包括類別、介面、方法、委託等)看作模板可能會更好理解,模板中的變體部分將被作為參數傳進來的類別名稱所代替,從而得到一個新的類型定義。泛型是比較大的話題,在此不作詳細解析,有興趣者可以查閱相關資料。 

    C# 泛型集合類別使用十分的方便快速。在這篇隨筆裡面,我將用鍊錶來模擬c#中的List﹤T﹥ 類別的行為,廢話不多說,下面來看我的實現代碼,代碼中已經寫了註釋,所以不再對代碼進行額外的說明:

    using System.Collections;
    class MyList﹤T﹥
    {
    
    public MyList()
    {
    this .firstNode = null;
    this.count = 0;
    }
    //C# 泛型集合-得到List長度
      }
    //增加一個節點
    public void AddElement(T data)
    {
    MyListNode first = this.firstNode;
    if(first==null) count++;
    return;
    }
    while (first .next != null)
    {
    first = first.next;
    }
       //C# 泛型集合-刪除一個節點
    public bool Remove (T data)
    {
    MyListNode first = this.firstNode;
    if (first.data.Equals(data))
 . this.count--;
    return true;
    }
    while (first.next!=null)
    {
    if (first.next.data.Equals(資料)) this.count--;
    return true;
    }
    }
    return false;
    }
    //C 
    int innercount = 1;
    MyListNode first = this.firstNode;
if (index ﹥ count)
    {
    throw new Exception("Index out of boundary");
    } )
    {
    first = first.next;
    innercount++;
    }
return first.data;
    }
    }
    //在指定的索引上插入新的元素
    public void
    MyListNode first = this.firstNode;
    if (index ﹥ count)
    {
    throw new Exception("Index out of boundary");
    }
  ListNode(data);
    this.firstNode.next = first;
    }
    else
    {
    while (innercount ﹤ index - 1)
{
    first = first.next;
    innercount++;
    }
    next = newNode;
    }
    this.count++;
    }
    / /C# 泛型集合-刪除指定索引上的集合元素
    public void RemoveAtIndex(int index)
    {
    int innercount = 1;List. count)
    {
    throw new Exception ("Index out of boundary");
    }
    if (index == 1)
    {
      {
    while (innercount ﹤ index - 1)
    {
    first = first.next;
    innercount++;
    }
    first.next = first.next.next    C# 泛型集合-刪除集合中的所有元素
    public void RemoveAll( )
    {
    this.firstNode = null;
    this.count = 0;
    }
 
    {
    MyListNode first = this.firstNode;
    while (first!= null)
    {
    yield return first.data;
    first = first.next;🠎 private class MyListNode
    {
    public T data { get; set; }//節點上的元素值

public MyListNode next { get; set; }//節點的下一個節點
    public MyListNode(T nodeData)
    { 
    }
    }

    下面是C# 泛型集合對這個類比類別的使用:

    class Program
      MyList﹤string﹥ ml = new MyList﹤string﹥();
    ml.AddElement("xu");
    ml.AddElement("jin");
    ml.AddElement("lin");
  
    ml.InsertAtIndex(4, "fiercely");
    ml.RemoveAtIndex(2);
    ml.Remove("lin");   Console.WriteLine(s);
    }
    }
    }

    C# 泛型集合實例應用的基本內容向你介紹到這裡,希望對你了解和學習C# 泛型集合有所幫助。

 以上就是C# 泛型集合實例應用淺析的內容,更多相關內容請關注PHP中文網(www.php.cn)!




相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!