溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#泛型集合實例怎么用

發布時間:2021-12-01 10:00:07 來源:億速云 閱讀:147 作者:小新 欄目:編程語言

小編給大家分享一下C#泛型集合實例怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

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

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

using System.Collections;   class MyList﹤T﹥  {  private MyListNode firstNode;//首節點  private int count;//C# 泛型集合-節點計數     public MyList()  {  this.firstNode = null;  this.count = 0;  }  //C# 泛型集合-得到List長度  public int GetLength()  {  return this.count;  }   //增加一個節點  public void AddElement(T data)  {  MyListNode first = this.firstNode;  if(first==null)  {  this.firstNode=new MyListNode(data);  this.count++;  return;  }  while (first.next != null)  {  first = first.next;  }  first.next = new MyListNode(data);  this.count++;  }   //C# 泛型集合-刪除一個節點  public bool Remove(T data)  {  MyListNode first = this.firstNode;  if (first.data.Equals(data))  {  this.firstNode = first.next;  this.count--;  return true;  }  while (first.next!=null)  {  if (first.next.data.Equals(data))  {  first.next = first.next.next;  this.count--;  return true;  }  }  return false;  }   //C# 泛型集合-得到指定索引上的集合元素  public T GetAtIndex(int index)  {  int innercount = 1;  MyListNode first = this.firstNode;  if (index ﹥ count)  {  throw new Exception("Index out of boundary");  }  else {  while (innercount ﹤ index)  {  first = first.next;  innercount++;  }  return first.data;  }  }   //在指定的索引上插入新的元素  public void InsertAtIndex(int index,T data)  {  int innercount = 1;  MyListNode first = this.firstNode;  if (index ﹥ count)  {  throw new Exception("Index out of boundary");  }  if (index == 1)  {  this.firstNode = new MyListNode(data);  this.firstNode.next = first;  }  else {  while (innercount ﹤ index - 1)  {  first = first.next;  innercount++;  }  MyListNode newNode = new MyListNode(data);  newNode.next = first.next;  first.next = newNode;  }  this.count++;  }   //C# 泛型集合-刪除指定索引上的集合元素  public void RemoveAtIndex(int index)  {  int innercount = 1;  MyListNode first = this.firstNode;  if (index ﹥ count)  {  throw new Exception("Index out of boundary");  }  if (index == 1)  {  this.firstNode = first.next;  }  else {  while (innercount ﹤ index - 1)  {  first = first.next;  innercount++;  }  first.next = first.next.next;  }  this.count--;  }   //C# 泛型集合-刪除集合中的所有元素  public void RemoveAll()  {  this.firstNode = null;  this.count = 0;  }   //為實現該集合類能用foreach進行遍歷  public IEnumerator GetEnumerator()  {  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)  {  this.data = nodeData;  this.next = null;  }  }  }

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

class Program  {  static void Main(string[] args)  {  MyList﹤string﹥ ml = new MyList﹤string﹥();  ml.AddElement("xu");  ml.AddElement("jin");  ml.AddElement("lin");  ml.AddElement("love");  ml.AddElement("jasmine");  ml.InsertAtIndex(4, "fiercely");  ml.RemoveAtIndex(2);  ml.Remove("lin");  foreach (string s in ml)  {  Console.WriteLine(s);  }  }  }

看完了這篇文章,相信你對“C#泛型集合實例怎么用”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女