泛型类就类似于一个模板,可以在需要时为这个模板传入任何我们需要的类型。
T是一个占位符,在.Net中,它叫做类型参数 (Type Parameter)。
泛型的效率比ArrayList的效率高,跟具体类型的数组差不多
方法:
public int[] BubbleUp(int[] arr) { for (int i = 0; i < arr.Length; i++) { for (int j = i + 1; j < arr.Length; j++) { if (arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; }
public T[] BubbleUp(T[] arr) { for (int i = 0; i < arr.Length; i++) { for (int j = i + 1; j < arr.Length; j++) { if (arr[i] > arr[j]) { T temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; }
上面这个方法运行时,T类型没有‘>’操作符
public class SortHelper { public void BubbleSort(T[] array){ // 方法实现体 }}
public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { SortHelper sorter = new SortHelper(); int[] array = { 8, 1, 4, 7, 3 }; sorter.BubbleSort (array); }}public class SortHelper { public void BubbleSort(T[] array) { int length = array.Length; for (int i = 0; i < length; i++) { HttpContext.Current.Response.Write(array[i].ToString() + " "); } }}
类:
public class SortHelper{ public void BubbleSort(T[] array){ // 方法实现体 }}
我们在类名称的后面加了一个尖括号,使用这个尖括号来传递我们的占位符,也就是类型参数。
SortHelper sorter = new SortHelper ();int[] array = { 8, 1, 4, 7, 3 };sorter.BubbleSort(array);
SortHelpersorter = new SortHelper ();byte [] array = { 8, 1, 4, 7, 3 };sorter.BubbleSort(array);
类型参数约束
public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { Book[] bookArray = new Book[2]; Book book1 = new Book(124, ".Net之美"); Book book2 = new Book(45, "C# 3.0揭秘"); bookArray[0] = book1; bookArray[1] = book2; SortHelpersorter = new SortHelper (); sorter.BubbleSort(bookArray); foreach (Book b in bookArray) { Response.Write(String.Format("Id:{0}", b.Id)); Response.Write(String.Format("Title:{0} ", b.Title)); } }}public class SortHelper 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; } } } }}public class Book : IComparable{ private int id; private string title; public Book() { } public Book(int id, string title) { this.id = id; this.title = title; } public int Id { get { return id; } set { id = value; } } public string Title { get { return title; } set { title = value; } } public int CompareTo(object obj) { Book book2 = (Book)obj; return this.Id.CompareTo(book2.Id); }}
方法参数约束
public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { Book[] bookArray = new Book[2]; Book book1 = new Book(124, ".Net之美"); Book book2 = new Book(45, "C# 3.0揭秘"); bookArray[0] = book1; bookArray[1] = book2; SortHelper sorter = new SortHelper(); sorter.BubbleSort(bookArray); foreach (Book b in bookArray) { Response.Write(String.Format("Id:{0}", b.Id)); Response.Write(String.Format("Title:{0} ", b.Title)); } }}public class SortHelper { public void BubbleSort (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; } } } }}public class Book : IComparable{ private int id; private string title; public Book() { } public Book(int id, string title) { this.id = id; this.title = title; } public int Id { get { return id; } set { id = value; } } public string Title { get { return title; } set { title = value; } } public int CompareTo(object obj) { Book book2 = (Book)obj; return this.Id.CompareTo(book2.Id); }}
没有约束
public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { Book[] bookArray = new Book[2]; Book book1 = new Book(124, ".Net之美"); Book book2 = new Book(45, "C# 3.0揭秘"); bookArray[0] = book1; bookArray[1] = book2; SortHelper sorter = new SortHelper(); sorter.BubbleSort(bookArray); foreach (Book b in bookArray) { Response.Write(String.Format("Id:{0}", b.Id)); Response.Write(String.Format("Title:{0} ", b.Title)); } }}public class SortHelper { 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; //} //} } }}public class Book : IComparable{ private int id; private string title; public Book() { } public Book(int id, string title) { this.id = id; this.title = title; } public int Id { get { return id; } set { id = value; } } public string Title { get { return title; } set { title = value; } } public int CompareTo(object obj) { Book book2 = (Book)obj; return this.Id.CompareTo(book2.Id); }}
http://www.tracefact.net/CSharp-Programming/Csharp-generics.aspx