根據我的理解,什么是泛型?泛型就是一種開放型類型,只有到編譯的時候才能確定它的類型,不像傳統的封閉類型;根據百度百科解釋,即為泛型是程序設計語言的一種特性。允許程序員在強類型程序設計語言中編寫代碼時定義一些可變部分,那些部分在使用前必須作出指明。各種程序設計語言和其編譯器、運行環境對泛型的支持均不一樣。將類型參數化以達到代碼復用提高軟件開發工作效率的一種數據類型。泛型類是引用類型,是堆對象,主要是引入了類型參數這個概念。為了加深理解,我會以一個自定義集合來講解泛型:
如果要定義一個自定義集合,無非是用一個類來模擬,類里面的核心屬性就是數組,其他還有add、remove、索引器等成員,廢話不說,直接上代碼
 1 public class IntList 2 { 3     int[] arr = new int[2]; 4     int index = 0; 5     public int count = 0; 6     public void Add(int num) 7     { 8         if (index >= arr.Length) 9         {10             int[] tempArr = new int[arr.Length * 2];11             arr.CopyTo(tempArr, 0);12             arr = tempArr;13         }14         arr[index] = num;15         index++;16         count++;17     }18     public int this[int index]19     {20         get21         {22             if (index < arr.Length)23             {24                 return arr[index];25             }26             else27             {28                 throw new Exception("數組越界");29             }30         }31         set32         {33             if (index < arr.Length)34             {35                 arr[index] = value;36             }37             else38             {39                 throw new Exception("賦值索引越界");40             }41         }42     }43 }View Code從代碼中,大家可以看出,在add方法中,當索引長度大于等于數組長度時,數組就會擴容,實質上是實例化一個新數組,然后把元數組數據拷貝到新數組中,再把新數組的引用賦給原數組,我們可以查看一下實際運行結果

1 IntList intlist = new IntList();2 intlist.Add(2);3 intlist.Add(3);4 intlist.Add(4);5 intlist.Add(5);6 Console.WriteLine(intlist.count);View Code
這時候,我們算基本完成了自定義集合的功能,等等,這只是int類型的集合,那么,如果想定義string或者其他類型的集合功能,我們又應該怎么辦呢,難道每一種類型都要定義一個類嗎,這樣做其實本身是可以的,可是我們的身為程序猿的尊嚴不能丟棄,于是這時候一個救星出現了,它就叫做泛型類,上代碼:

 1 public class MyList<T> 2 { 3     T[] arr = new T[2]; 4     int index = 0; 5     public int count = 0; 6     public void Add(T num) 7     { 8         if (index >= arr.Length) 9         {10             T[] tempArr = new T[arr.Length * 2];11             arr.CopyTo(tempArr, 0);12             arr = tempArr;13         }14         arr[index] = num;15         index++;16         count++;17     }18     public T this[int index]19     {20         get21         {22             if (index < arr.Length)23             {24                 return arr[index];25             }26             else27             {28                 throw new Exception("數組越界");29             }30         }31         set32         {33             if (index < arr.Length)34             {35                 arr[index] = value;36             }37             else38             {39                 throw new Exception("賦值索引越界");40             }41         }42     }43 }View Code乍一看,這段代碼貌似和第一段代碼沒什么不同,仔細一看,發現int類型不見了,轉而是用了一個字母T來代替int類型,還有,貌似定義類的時候多了那么一點點東西。public class MyList<T>,這個T實際上就是一個類型占位符,你可以用任意字符來代替它,但習慣上,都會用T來占位,這樣,我們大體上就大功告成了,來看下運行結果

1 MyList<string> mlist = new MyList<string>();2 mlist.Add("1");3 mlist.Add("2m");View Code  嗯,貌似大體上功能都搞定了,但是真的結束了嗎?答案是No!  我們今天主要是探討一下泛型類的使用方法,而不僅僅是會用,下邊我將用代碼的形式為大家拓展一下,沒錯,下邊要講的就是泛型類型限定,先定義出來我們所需要的類:
1 public interface ipig2 {3 }4 public class Pig : IPig5 {6 }7 public class SmallPig : Pig8 {9 }View Code

1 public class PigList1<T> where T : struct  //泛型類型只能為值類型2 {3 }View Code
1 public class PigList2<T> where T : class  //泛型類型只能為引用類型2 {3 }View Code
1 public class PigList3<T> where T : Pig  //泛型類型只能為指定類型或者指定類型的子類2 {3 }View Code
1 public class PigList4<T> where T : IPig  //泛型類型只能為實現指定接口的類,或者實現指定接口的子類2 {3 }View Code新聞熱點
疑難解答