C#泛型實例詳解。本站提示廣大學習愛好者:(C#泛型實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#泛型實例詳解正文
本文以實例情勢講述了C#泛型的用法,有助於讀者深刻懂得C#泛型的道理,詳細剖析以下:
起首須要明確甚麼時刻應用泛型:
當針對分歧的數據類型,采取類似的邏輯算法,為了不反復,可以斟酌應用泛型。
1、針對類的泛型
針對分歧類型的數組,寫一個針對數組的"冒泡排序"。
1.思緒
● 針對類的泛型,泛型打在類旁。
● 因為在"冒泡排序"中須要對元素停止比擬,所以泛型要束縛成完成IComparable接口。
class Program
{
static void Main(string[] args)
{
SortHelper<int> isorter = new SortHelper<int>();
int[] iarray = {8, 7, 1, 2, 12};
isorter.BubbleSort(iarray);
foreach (int item in iarray)
{
Console.Write(item+ ", ");
}
Console.ReadKey();
}
}
public class SortHelper<T> where T : IComparable
{
public void BubbleSort(T[] array)
{
int length = array.Length;
for (int i = 0; i <= length -2; i++)
{
for (int j = length - 1; j >= 1; j--)
{
if (array[j].CompareTo(array[j-1]) < 0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
}
運轉成果以下圖所示:

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接口。

2、自界說一個類,使之也能完成冒泡算法
冒泡算法觸及到元素比擬,所以自界說類必需完成IComparable接口。
class Program
{
static void Main(string[] args)
{
Book[] bookArray = new Book[2];
Book book1 = new Book(100, "書一");
Book book2 = new Book(80, "書二");
bookArray[0] = book1;
bookArray[1] = book2;
Console.WriteLine("冒泡之前:");
foreach (Book b in bookArray)
{
Console.WriteLine("書名:{0},價錢:{1}", b.Title, b.Price);
}
SortHelper<Book> sorter = new SortHelper<Book>();
sorter.BubbleSort(bookArray);
Console.WriteLine("冒泡以後:");
foreach (Book b in bookArray)
{
Console.WriteLine("書名:{0},價錢:{1}", b.Title, b.Price);
}
Console.ReadKey();
}
}
public class SortHelper<T> where T : IComparable
{
public void BubbleSort(T[] array)
{
int length = array.Length;
for (int i = 0; i <= length -2; i++)
{
for (int j = length - 1; j >= 1; j--)
{
if (array[j].CompareTo(array[j-1]) < 0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
}
//自界說類完成IComparable接口
public class Book : IComparable
{
private int price;
private string title;
public Book(){}
public Book(int price, string title)
{
this.price = price;
this.title = title;
}
public int Price
{
get { return this.price; }
}
public string Title
{
get { return this.title; }
}
public int CompareTo(object obj)
{
Book book = (Book)obj;
return this.Price.CompareTo(book.Price);
}
}
運轉成果以下圖所示:

3、針對辦法的泛型
持續下面的例子,自界說一個類,並界說泛型辦法。
//辦法泛型
public class MethodSortHelper
{
public void BubbleSort<T>(T[] array) where T : IComparable
{
int length = array.Length;
for (int i = 0; i <= length - 2; i++)
{
for (int j = length - 1; j >= 1; j--)
{
if (array[j].CompareTo(array[j - 1]) < 0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
}
主法式以下:
class Program
{
static void Main(string[] args)
{
Book[] bookArray = new Book[2];
Book book1 = new Book(100, "書一");
Book book2 = new Book(80, "書二");
bookArray[0] = book1;
bookArray[1] = book2;
Console.WriteLine("冒泡之前:");
foreach (Book b in bookArray)
{
Console.WriteLine("書名:{0},價錢:{1}", b.Title, b.Price);
}
MethodSortHelper sorter = new MethodSortHelper();
sorter.BubbleSort<Book>(bookArray);
Console.WriteLine("冒泡以後:");
foreach (Book b in bookArray)
{
Console.WriteLine("書名:{0},價錢:{1}", b.Title, b.Price);
}
Console.ReadKey();
}
}
運轉成果以下圖所示:

別的,應用泛型辦法的時刻,除按以下:
MethodSortHelper sorter = new MethodSortHelper(); sorter.BubbleSort<Book>(bookArray);
還可以如許寫:
MethodSortHelper sorter = new MethodSortHelper();
sorter.BubbleSort(bookArray);
可見,泛型辦法可以依據數組實例隱式揣摸泛型能否知足前提。
4、泛型的其它長處
1.防止隱式裝箱和拆箱
以下包括隱式裝箱和拆箱:
ArrayList list = new ArrayList();
for(int i = 0; i < 3; i++)
{
list.Add(i); //Add吸收的參數類型是援用類型object,這裡包括了隱式裝箱
}
for(int i = 0; i < 3; i++)
{
int value = (int)list[i]; //援用類型強轉成值類型,拆箱
Console.WriteLine(value);
}
應用泛型防止隱式裝箱和拆箱:
List<int> list = new List<int>();
for(int i = 0; i < 3; i++)
{
list.Add(i);
}
for(int i = 0; i < 3; i++)
{
int value = list[i];
Console.WriteLine(value);
}
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];
5、應用泛型的技能
1.在以後文件中給泛型取別號
using IntList = List<int>; IntList list = new IntList(); list.Add(1);
2.在分歧文件中應用泛型別號,界說一個類派生於泛型
public class IntList : List<int>{}