C#泛型實例詳解


本文以實例形式講述了C#泛型的用法,有助於讀者深入理解C#泛型的原理,具體分析如下:

首先需要明白什么時候使用泛型:

當針對不同的數據類型,采用相似的邏輯算法,為了避免重復,可以考慮使用泛型。

一、針對類的泛型

針對不同類型的數組,寫一個針對數組的"冒泡排序"。

1.思路

● 針對類的泛型,泛型打在類旁。 
● 由於在"冒泡排序"中需要對元素進行比較,所以泛型要約束成實現IComparable接口。

 1 class Program
 2 {
 3   static void Main(string[] args)
 4   {
 5     SortHelper<int> isorter = new SortHelper<int>();
 6     int[] iarray = {8, 7, 1, 2, 12};
 7     isorter.BubbleSort(iarray);
 8     foreach (int item in iarray)
 9     {
10       Console.Write(item+ ", ");
11     }
12     Console.ReadKey();
13   }
14 }
15  
16 public class SortHelper<T> where T : IComparable
17 {
18   public void BubbleSort(T[] array)
19   {
20     int length = array.Length;
21     for (int i = 0; i <= length -2; i++)
22     {
23       for (int j = length - 1; j >= 1; j--)
24       {
25         if (array[j].CompareTo(array[j-1]) < 0)
26         {
27           T temp = array[j];
28           array[j] = array[j - 1];
29           array[j - 1] = temp;
30         }
31       }
32     }
33   }
34 }

 運行結果如下圖所示:

2.關於泛型約束

where T : IComparable 把T約束為實現IComparable接口 
where T : class 
where T : struct 
where T : IComparable, new() 約束泛型必須有構造函數

3.關於冒泡算法

● 之所以for (int i = 0; i <= length -2; i++),這是邊界思維,比如有一個長度為5的數組,如果0號位元素最終調換到4號位,每次調一個位,需要經過4次才能到4號位,即for(int i = 0; i <= 5-2, i++),i依次為0, 1, 2, 4,期間經歷了4次。

● 至於for (int j = length - 1; j >= 1; j--)循環,即遍歷從最后一個元素開始到索引為1的元素,每次與前一個位置上的元素比較。

4.關於比較

int類型之所以能比較,是因為int類型也實現了IComparable接口。

byte類型也一樣實現了IComparable接口。

二、自定義一個類,使之也能實現冒泡算法

冒泡算法涉及到元素比較,所以自定義類必須實現IComparable接口。

 1 class Program
 2 {
 3   static void Main(string[] args)
 4   {
 5     Book[] bookArray = new Book[2];
 6     Book book1 = new Book(100, "書一");
 7     Book book2 = new Book(80, "書二");
 8     bookArray[0] = book1;
 9     bookArray[1] = book2;
10  
11     Console.WriteLine("冒泡之前:");
12     foreach (Book b in bookArray)
13     {
14       Console.WriteLine("書名:{0},價格:{1}", b.Title, b.Price);
15     }
16  
17     SortHelper<Book> sorter = new SortHelper<Book>();
18     sorter.BubbleSort(bookArray);
19     Console.WriteLine("冒泡之后:");
20     foreach (Book b in bookArray)
21     {
22       Console.WriteLine("書名:{0},價格:{1}", b.Title, b.Price);
23     }
24     Console.ReadKey();
25   }
26 }
27  
28 public class SortHelper<T> where T : IComparable
29 {
30   public void BubbleSort(T[] array)
31   {
32     int length = array.Length;
33     for (int i = 0; i <= length -2; i++)
34     {
35       for (int j = length - 1; j >= 1; j--)
36       {
37         if (array[j].CompareTo(array[j-1]) < 0)
38         {
39           T temp = array[j];
40           array[j] = array[j - 1];
41           array[j - 1] = temp;
42         }
43       }
44     }
45   }
46 }
47  
48 //自定義類實現IComparable接口
49 public class Book : IComparable
50 {
51   private int price;
52   private string title;
53  
54   public Book(){}
55  
56   public Book(int price, string title)
57   {
58     this.price = price;
59     this.title = title;
60   }
61  
62   public int Price
63   {
64     get { return this.price; }
65   }
66  
67   public string Title
68   {
69     get { return this.title; }
70   }
71  
72   public int CompareTo(object obj)
73   {
74     Book book = (Book)obj;
75     return this.Price.CompareTo(book.Price);
76   }
77 }

 運行結果如下圖所示:

三、針對方法的泛型

繼續上面的例子,自定義一個類,並定義泛型方法。

 1 //方法泛型
 2 public class MethodSortHelper
 3 {
 4   public void BubbleSort<T>(T[] array) where T : IComparable
 5   {
 6     int length = array.Length;
 7     for (int i = 0; i <= length - 2; i++)
 8     {
 9       for (int j = length - 1; j >= 1; j--)
10       {
11         if (array[j].CompareTo(array[j - 1]) < 0)
12         {
13           T temp = array[j];
14           array[j] = array[j - 1];
15           array[j - 1] = temp;
16         }
17       }
18     }
19   }
20 }

 主程序如下:

 1 class Program
 2 {
 3   static void Main(string[] args)
 4   {
 5     Book[] bookArray = new Book[2];
 6     Book book1 = new Book(100, "書一");
 7     Book book2 = new Book(80, "書二");
 8     bookArray[0] = book1;
 9     bookArray[1] = book2;
10  
11     Console.WriteLine("冒泡之前:");
12     foreach (Book b in bookArray)
13     {
14       Console.WriteLine("書名:{0},價格:{1}", b.Title, b.Price);
15     }
16  
17     MethodSortHelper sorter = new MethodSortHelper();
18     sorter.BubbleSort<Book>(bookArray);
19     Console.WriteLine("冒泡之后:");
20     foreach (Book b in bookArray)
21     {
22       Console.WriteLine("書名:{0},價格:{1}", b.Title, b.Price);
23     }
24     Console.ReadKey();
25   }
26 } 

 運行結果如下圖所示:

另外,使用泛型方法的時候,除了按以下:

還可以這樣寫:  

可見,泛型方法可以根據數組實例隱式推斷泛型是否滿足條件。

四、泛型的其它優點

1.避免隱式裝箱和拆箱

以下包含隱式裝箱和拆箱:

 1 ArrayList list = new ArrayList();
 2 for(int i = 0; i < 3; i++)
 3 {
 4   list.Add(i); //Add接收的參數類型是引用類型object,這里包含了隱式裝箱
 5 }
 6 for(int i = 0; i < 3; i++)
 7 {
 8   int value = (int)list[i]; //引用類型強轉成值類型,拆箱
 9   Console.WriteLine(value);
10 }

 使用泛型避免隱式裝箱和拆箱:

 1 List<int> list = new List<int>();
 2 for(int i = 0; i < 3; i++)
 3 {
 4   list.Add(i);
 5 }
 6 for(int i = 0; i < 3; i++)
 7 {
 8   int value = list[i];
 9   Console.WriteLine(value);
10 }

 

2.能在編譯期間及時發現錯誤

不使用泛型,在編譯期不會報錯的一個例子:

ArrayList list = new ArrayList();
int i = 100;
list.Add(i);
string value = (string)list[0];

  使用泛型,在編譯期及時發現錯誤:

List<int> list = new List<int>();
int i = 100;
list.Add(i);
string value = (string)list[0];

五、使用泛型的技巧

1.在當前文件中給泛型取別名

using IntList = List<int>;
IntList list = new IntList();
list.Add(1);

2.在不同文件中使用泛型別名,定義一個類派生於泛型

public class IntList : List<int>{}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM