zoukankan      html  css  js  c++  java
  • 关于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#泛型作用的简单说明就到这里。

  • 相关阅读:
    jquery.validate验证表单配合回调提交和h5.storage本地保存笔记
    datetimepicker时间控件
    画图工具除了echarts我使用highcharts
    jquery.bootpag分页控件
    时间插件--做到前几个月和后个几月的设置
    websocket小荔枝
    jquery请求解析xml
    【leetcode】Combinations
    【leetcode】Balanced Binary Tree
    【leetcode刷题笔记】Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/4595200.html
Copyright © 2011-2022 走看看