本文以实例形式讲述了C#泛型的用法,有助于读者深入理解C#泛型的原理,具体分析如下:

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

当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型。

一、针对类的泛型

针对不同类型的数组,写一个针对数组的"冒泡排序"。

1.思路

● 针对类的泛型,泛型打在类旁。 
● 由于在"冒泡排序"中需要对元素进行比较,所以泛型要约束成实现IComparable接口。

 class Program
{
static void Main(string[] args)
{
SortHelper<int> isorter = new SortHelper<int>();
int[] iarray = {, , , , };
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 = ; i <= length -; i++)
{
for (int j = length - ; j >= ; j--)
{
if (array[j].CompareTo(array[j-]) < )
{
T temp = array[j];
array[j] = array[j - ];
array[j - ] = 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接口。

二、自定义一个类,使之也能实现冒泡算法

冒泡算法涉及到元素比较,所以自定义类必须实现IComparable接口。

 class Program
{
static void Main(string[] args)
{
Book[] bookArray = new Book[];
Book book1 = new Book(, "书一");
Book book2 = new Book(, "书二");
bookArray[] = book1;
bookArray[] = 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 = ; i <= length -; i++)
{
for (int j = length - ; j >= ; j--)
{
if (array[j].CompareTo(array[j-]) < )
{
T temp = array[j];
array[j] = array[j - ];
array[j - ] = 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);
}
}

运行结果如下图所示:

三、针对方法的泛型

继续上面的例子,自定义一个类,并定义泛型方法。

 //方法泛型
public class MethodSortHelper
{
public void BubbleSort<T>(T[] array) where T : IComparable
{
int length = array.Length;
for (int i = ; i <= length - ; i++)
{
for (int j = length - ; j >= ; j--)
{
if (array[j].CompareTo(array[j - ]) < )
{
T temp = array[j];
array[j] = array[j - ];
array[j - ] = temp;
}
}
}
}
}

主程序如下:

 class Program
{
static void Main(string[] args)
{
Book[] bookArray = new Book[];
Book book1 = new Book(, "书一");
Book book2 = new Book(, "书二");
bookArray[] = book1;
bookArray[] = 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();
}
}

运行结果如下图所示:

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

还可以这样写:

可见,泛型方法可以根据数组实例隐式推断泛型是否满足条件。

四、泛型的其它优点

1.避免隐式装箱和拆箱

以下包含隐式装箱和拆箱:

 ArrayList list = new ArrayList();
for(int i = ; i < ; i++)
{
list.Add(i); //Add接收的参数类型是引用类型object,这里包含了隐式装箱
}
for(int i = ; i < ; i++)
{
int value = (int)list[i]; //引用类型强转成值类型,拆箱
Console.WriteLine(value);
}

使用泛型避免隐式装箱和拆箱:

 List<int> list = new List<int>();
for(int i = ; i < ; i++)
{
list.Add(i);
}
for(int i = ; i < ; 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];

五、使用泛型的技巧

1.在当前文件中给泛型取别名

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

2.在不同文件中使用泛型别名,定义一个类派生于泛型

public class IntList : List<int>{}

  

最新文章

  1. java List的排序
  2. node.js中buffer需要知道的一些点
  3. sublime text 3.0 安装 HTML-CSS-JS Prettify
  4. codeforces 474D.Flowers 解题报告
  5. php linux部署相关
  6. json所需jar包 &amp; js解析后台json数据
  7. Android中如何判断是否联网
  8. Java基础篇Socket网络编程中的应用实例
  9. Java学习笔记2(输入与随机数简单介绍)
  10. 栅栏(fence)
  11. 错误:One or more post-processing actions failed. Consult the OPP service log for details
  12. 团队项目第二阶段个人进展——Day5
  13. 20175306 迭代和JDB调试
  14. org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found :
  15. Windows下利用MKL加速caffe,与openblas比较
  16. 利用 SPICE 分析理解心电图前端中的右腿驱动
  17. 062、如何使用flannel host-gw backend(2019-04-02 周二)
  18. Base 64 加密、解密
  19. jq回车触发绑定点击事件
  20. Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性

热门文章

  1. 【halcon】算子
  2. 最小生成树(II)与Kruskal算法
  3. react和vue的区别
  4. leetcode个人题解——two sum
  5. 多个表单数据提交下的serialize()应用
  6. phpcms黄页,不能选择行业。解决办法
  7. centos升级python(从2.6.6升级到2.7.8)
  8. Python文件操作:同一个文件进行内容替换
  9. 【SQLAlchemy】SQLAlchemy技术文档(中文版)(上)
  10. 【codevs3160】最长公共子串 后缀数组