博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 泛型
阅读量:7195 次
发布时间:2019-06-29

本文共 6877 字,大约阅读时间需要 22 分钟。

 泛型类就类似于一个模板,可以在需要时为这个模板传入任何我们需要的类型。

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);
SortHelper
sorter = 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;        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
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

转载地址:http://wevkm.baihongyu.com/

你可能感兴趣的文章
Gradle Build速度加快终极方法(android studio)
查看>>
CentOS6.8安装360 pika
查看>>
scu oj 4442 Party(2015年四川省acm程序设计竞赛)
查看>>
hdu1133 Buy the Ticket (卡兰特数应用+java大数)
查看>>
Servlet -- 中文乱码解决
查看>>
【案例实战】餐饮企业分店財务数据分析系统解决方式:系统功能开发
查看>>
POJ 3154 Graveyard【多解,数论,贪心】
查看>>
C++ Primer 学习笔记与思考_7 void和void*指针的使用方法
查看>>
&lt;!DOCTYPE&gt;奇葩的问题
查看>>
使用Gitolite搭建Gitserver
查看>>
【Maven】Snapshot和Release版本的区别
查看>>
FZU1920 Left Mouse Button(dfs)
查看>>
迭代器模式
查看>>
poj 1979 dfs
查看>>
(转)论python工厂函数与内建函数
查看>>
HDU1212 Big Number 【同余定理】
查看>>
HDU1069(还是dp基础)
查看>>
用RotateDrawable实现网易云音乐唱片机效果
查看>>
Mac OS X各版本号的历史费用和升级关系
查看>>
Android -- AsyncTask源码解析
查看>>