关于C#泛型作用的简单说明

开发 后端
本文通过介绍泛型的概念,以代码对C#泛型的作用作了简单说明,希望对大家有用。

泛型:即通过参数化类型来实现在同一份代码上操作多种数据类型。泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用。

C#泛型的作用概述

C#泛型赋予了代码更强的类型安全,更好的复用,更高的效率,更清晰的约束。

在一个方法中,一个变量的值是可以作为参数,但其实这个变量的类型本身也可以作为参数。泛型允许我们在调用的时候再指定这个类型参数是什么。在.net中,泛型能够给我们带来的两个明显的好处是--类型安全和减少装箱、拆箱。

在一个方法中,一个变量的值是可以作为参数,但其实这个变量的类型本身也可以作为参数。泛型允许我们在调用的时候再指定这个类型参数是什么。在.net中,泛型能够给我们带来的两个明显的好处是:类型安全和减少装箱、拆箱。

假设我们现在有一个人员集合:创建Person类

  1. public class Person  
  2. {  
  3. private string name;  
  4. private int age;  
  5.  
  6. public Person(string Name, int Age) //构造函数  
  7. {  
  8. this.age = Age;  
  9. this.name = Name;  
  10. }  
  11. public string Name  
  12. {  
  13. set { this.name = value; }  
  14. get { return name; }  
  15. }  
  16. public int Age  
  17. {  
  18. set { this.age = value; }  
  19. get { return age; }  
  20. }  

//我们在程序的入口点处运行 以下在集合中增加了一个其他类型的对象,但插入数据的时候并没有报错,编译也可以通过,但把“谁是功夫之王?”这样的字段转换成人员的时候出问题了,这说明ArrayList是类型不安全的。

  1. static void Main(string[] args)  
  2. {  
  3. ArrayList peoples = new ArrayList();  
  4. peoples.Add(new Person("成龙", 18));  
  5. peoples.Add(new Person("李小龙", 17));  
  6. peoples.Add("谁是功夫之王?");  
  7. foreach (Person person in peoples)  
  8. {  
  9. Console.WriteLine(person.Name + "今年" + person.Age + "岁。");  
  10. }  

//因此在此处中我们创建一个人员的泛型,当要向里面插入其他类型的时候,编译器就会报错

  1. static void Main(string[] args)  
  2. {  
  3. List< Person> peoples = new List< Person>();  
  4. peoples.Add(new Person("成龙", 18));  
  5. peoples.Add(new Person("李小龙", 17));  
  6. peoples.Add("谁是功夫之王?");  
  7. foreach (Person person in peoples)  
  8. {  
  9. Console.WriteLine(person.Name + "今年" + person.Age + "岁。");  
  10. }  

C#泛型的作用:排序

C#泛型作为一种集合,排序是不可或缺的。排序基于比较同,要排序,首先要比较。一个对象可以有多个比较规则,但只能有一个默认规则,默认规则放在定义该对象的类中。默认规则在CompareTo方法中定义,该方法属于IComparable< T>泛型接口。

  1. public class Person :IComparable< Person>   
  2. {  
  3.  
  4. 。。。。。。  
  5. public int CompareTo(Person p) //此处增加一个按年龄排序比较器  
  6. {  
  7. return this.Age - p.Age;  
  8. }  
  9. }  
  10.  
  11. static void Main(string[] args)  
  12. {  
  13. List< Person> peoples = new List< Person>();  
  14. peoples.Add(new Person("陈小龙", 18));  
  15. peoples.Add(new Person("李小龙", 17));  
  16. peoples.Add(new Person("房小龙", 23));  
  17. peoples.Add(new Person("张小龙", 42));  
  18. peoples.Sort(); //不带参数的Sort()方法对集合进行排序  
  19. foreach (Person person in peoples)  
  20. {  
  21. Console.WriteLine(person.Name + "今年" + person.Age + "岁。");  
  22. }  

好了,关于C#泛型作用的简单说明就到这里。

【编辑推荐】

  1. C#实现多语言界面程序的方法介绍
  2. 介绍C#构造函数的使用方法
  3. C#多态性的概念及其应用
  4. 浅析C# treeview控件的使用方法
  5. 总结C#获取当前路径的7种方法
责任编辑:book05 来源: hi.baidu
相关推荐

2009-09-02 17:38:16

C#泛型支持

2009-09-02 18:03:19

C#实现泛型类

2009-08-24 14:43:35

C# 泛型

2009-06-24 10:25:25

C#泛型

2009-08-26 09:36:03

C#泛型

2009-09-01 16:14:11

C#泛型

2009-08-24 10:29:39

C# 泛型

2013-03-20 09:27:33

C#泛型

2009-08-24 18:15:24

C# Dictiona

2009-08-24 15:12:13

C# 泛型接口

2009-08-24 14:51:25

C# 泛型泛型类型

2009-08-24 15:38:21

C# 泛型数组

2009-08-24 14:20:13

C# 强制类型转换

2010-01-12 18:27:58

C++代码

2009-08-24 10:07:57

C#泛型处理

2010-06-18 17:13:07

Linux anacr

2010-06-13 15:10:19

Linux 查看进程

2009-08-24 17:27:05

C#泛型应用

2009-08-24 16:39:19

C# 泛型应用

2009-08-24 17:58:19

C# 泛型集合
点赞
收藏

51CTO技术栈公众号