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#泛型作用的简单说明就到这里。

  • 相关阅读:
    姐姐的vue(1)
    LeetCode 64. Minimum Path Sum 20170515
    LeetCode 56. 56. Merge Intervals 20170508
    LeetCode 26. Remove Duplicates from Sorted Array
    LeetCode 24. Swap Nodes in Pairs 20170424
    LeetCode 19. Remove Nth Node From End of List 20170417
    LeetCode No.9 Palindrome Number 20170410
    LeetCode No.8. String to Integer (atoi) 2017/4/10(补上一周)
    LeetCode No.7 Reverse Integer 2017/3/27
    LeetCode No.4 Median of Two Sorted Arrays 20170319
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/4595200.html
Copyright © 2011-2022 走看看