zoukankan      html  css  js  c++  java
  • C#中的泛型I

    C#中的泛型I
    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性。泛型为.NET框架引入了类型参数(type parameters)的概念。类型参数使得设计类和方法时,不必确定一个或多个具体参数,其的具体参数可延迟到客户代码中声明、实现。这意味着使用泛型的类型参数T,写一个类MyList<T>,客户代码可以这样调用:MyList<INT>, MyList<STRING>或 MyList<MYCLASS>。这避免了运行时类型转换或装箱操作的代价和风险。
    目录
    一、泛型概述
    二、泛型的优点
    三、泛型类型参数
    四、类型参数的约束
    五、泛型类
    六、泛型接口


    一、泛型概述

    泛型类和泛型方法兼复用性、类型安全和高效率于一身,是与之对应的非泛型的类和方法所不及。泛型广泛用于容器(collections)和对容器操作的方法中。.NET框架2.0的类库提供一个新的命名空间System.Collections.Generic,其中包含了一些新的基于泛型的容器类。要查找新的泛型容器类(collection classes)的示例代码,请参见基础类库中的泛型。当然,你也可以创建自己的泛型类和方法,以提供你自己的泛化的方案和设计模式,这是类型安全且高效的。下面的示例代码以一个简单的泛型链表类作为示范。(多数情况下,推荐使用由.NET框架类库提供的List类,而不是创建自己的表。)类型参数T在多处使用,具体类型通常在这些地方来指明表中元素的类型。类型参数T有以下几种用法:
    在AddHead方法中,作为方法参数的类型。
    在公共方法GetNext中,以及嵌套类Node的 Data属性中作为返回值的类型。
    在嵌套类中,作为私有成员data的类型。
    注意一点,T对嵌套的类Node也是有效的。当用一个具体类来实现MyList时——如MyList——每个出现过的T都要用int代替。
    using System;
    using System.Collections.Generic;
    public class MyList //type parameter T in angle brackets
    {
    private Node head;
    // The nested type is also generic on T.
    private class Node
    {
    private Node next;
    //T as private member data type:
    private T data;
    //T used in non-generic constructor:
    public Node(T t)
    {
    next = null;
    data = t;
    }
    public Node Next
    {
    get { return next; }
    set { next = value; }
    }
    //T as return type of property:
    public T Data
    {
    get { return data; }
    set { data = value; }
    }
    }
    public MyList()
    {
    head = null;
    }
    //T as method parameter type:
    public void AddHead(T t)
    {
    Node n = new Node(t);
    n.Next = head;
    head = n;
    }
    public IEnumerator GetEnumerator()
    {
    Node current = head;
    while (current != null)
    {
    yield return current.Data;
    current = current.Next;
    }
    }
    }
    下面的示例代码演示了客户代码如何使用泛型类MyList,来创建一个整数表。通过简单地改变参数的类型,很容易改写下面的代码,以创建字符串或其他自定义类型的表。
    class Program
    {
    static void Main(string[] args)
    {
    //int is the type argument.
    MyList list = new MyList();
    for (int x = 0; x < 10; x++)
    list.AddHead(x);
    foreach (int i in list)
    {
    Console.WriteLine(i);
    }
    Console.WriteLine("Done");
    }
    }

    二、泛型的优点
    针对早期版本的通用语言运行时和C#语言的局限,泛型提供了一个解决方案。以前类型的泛化(generalization)是靠类型与全局基类System.Object的相互转换来实现。.NET框架基础类库的ArrayList容器类,就是这种局限的一个例子。ArrayList是一个很方便的容器类,使用中无需更改就可以存储任何引用类型或值类型。
    //The .NET Framework 1.1 way of creating a list
    ArrayList list1 = new ArrayList();
    list1.Add(3);
    list1.Add(105);
    //...
    ArrayList list2 = new ArrayList();
    list2.Add("It is raining in Redmond.");
    list2.Add("It is snowing in the mountains.");
    //...
    但是这种便利是有代价的,这需要把任何一个加入ArrayList的引用类型或值类型都隐式地向上转换成System.Object。如果这些元素是值类型,那么当加入到列表中时,它们必须被装箱;当重新取回它们时,要拆箱。类型转换和装箱、拆箱的操作都降低了性能;在必须迭代(iterate)大容器的情况下,装箱和拆箱的影响可能十分显著。
    另一个局限是缺乏编译时的类型检查,当一个ArrayList把任何类型都转换为Object,就无法在编译时预防客户代码类似这样的操作:
    ArrayList list = new ArrayList();
    //Okay.
    list.Add(3);
    //Okay, but did you really want to do this?
    list.Add(."It is raining in Redmond.");
    int t = 0;
    //This causes an InvalidCastException to be returned.
    foreach(int x in list)
    {
    t += x;
    }
    虽然这样完全合法,并且有时是有意这样创建一个包含不同类型元素的容器,但是把string和int变量放在一个ArrayList中,几乎是在制造错误,而这个错误直到运行的时候才会被发现。
    在1.0版和1.1版的C#语言中,你只有通过编写自己的特定类型容器,才能避免.NET框架类库的容器类中泛化代码(generalized code)的危险。当然,因为这样的类无法被其他的数据类型复用,也就失去泛型的优点,你必须为每个需要存储的类型重写该类。
    ArrayList和其他相似的类真正需要的是一种途径,能让客户代码在实例化之前指定所需的特定数据类型。这样就不需要向上类型转换为Object,而且编译器可以同时进行类型检查。换句话说,ArrayList需要一个类型参数。这正是泛型所提供的。在System.Collections.Generic命名空间中的泛型List容器里,同样是把元素加入容器的操作,类似这样:
    The .NET Framework 2.0 way of creating a list
    List list1 = new List();
    //No boxing, no casting:
    list1.Add(3);
    //Compile-time error:
    list1.Add("It is raining in Redmond.");
    与ArrayList相比,在客户代码中唯一增加的List语法是声明和实例化中的类型参数。代码略微复杂的回报是,你创建的表不仅比ArrayList更安全,而且明显地更加快速,尤其当表中的元素是值类型的时候。

    三、泛型类型参数

    在泛型类型或泛型方法的定义中,类型参数是一个占位符(placeholder),通常为一个大写字母,如T。在客户代码声明、实例化该类型的变量时,把T替换为客户代码所指定的数据类型。泛型类,如泛型概述中给出的MyList类,不能用作as-is,原因在于它不是一个真正的类型,而更像是一个类型的蓝图。要使用MyList,客户代码必须在尖括号内指定一个类型参数,来声明并实例化一个已构造类型(constructed type)。这个特定类的类型参数可以是编译器识别的任何类型。可以创建任意数量的已构造类型实例,每个使用不同的类型参数,如下:
    MyList list1 = new MyList();
    MyList list2 = new MyList();
    MyList list3 = new MyList();
    在这些MyList的实例中,类中出现的每个T都将在运行的时候被类型参数所取代。依靠这样的替换,我们仅用定义类的代码,就创建了三个独立的类型安全且高效的对象。有关CLR执行替换的详细信息,请参见运行时中的泛型。

    四、类型参数的约束
    若要检查表中的一个元素,以确定它是否合法或是否可以与其他元素相比较,那么编译器必须保证:客户代码中可能出现的所有类型参数,都要支持所需调用的操作或方法。这种保证是通过在泛型类的定义中,应用一个或多个约束而得到的。一个约束类型是一种基类约束,它通知编译器,只有这个类型的对象或从这个类型派生的对象,可被用作类型参数。一旦编译器得到这样的保证,它就允许在泛型类中调用这个类型的方法。上下文关键字where用以实现约束。下面的示例代码说明了应用基类约束,为MyList类增加功能。
    public class Employee
    {
    public class Employee
    {
    private string name;
    private int id;
    public Employee(string s, int i)
    {
    name = s;
    id = i;
    }
    public string Name
    {
    get { return name; }
    set { name = value; }
    }
    public int ID
    {
    get { return id; }
    set { id = value; }
    }
    }
    }
    class MyList where T: Employee
    {
    //Rest of class as before.
    public T FindFirstOccurrence(string s)
    {
    T t = null;
    Reset();
    while (HasItems())
    {
    if (current != null)
    {
    //The constraint enables this:
    if (current.Data.Name == s)
    {
    t = current.Data;
    break;
    }
    else
    {
    current = current.Next;
    }
    } //end if
    } // end while
    return t;
    }
    }
    约束使得泛型类能够使用Employee.Name属性,因为所有为类型T的元素,都是一个Employee对象或是一个继承自Employee的对象。
    同一个类型参数可应用多个约束。约束自身也可以是泛型类,如下:
    class MyList where T: Employee, IEmployee, IComparable, new()
    {...}
    下表列出了五类约束:
    约束
    描述

    where T: struct
    类型参数必须为值类型。

    where T : class
    类型参数必须为类型。

    where T : new()
    类型参数必须有一个公有、无参的构造函数。当于其它约束联合使用时,new()约束必须放在最后。

    where T :
    类型参数必须是指定的基类型或是派生自指定的基类型。

    where T :
    类型参数必须是指定的接口或是指定接口的实现。可以指定多个接口约束。接口约束也可以是泛型的。

    类型参数的约束,增加了可调用的操作和方法的数量。这些操作和方法受约束类型及其派生层次中的类型的支持。因此,设计泛型类或方法时,如果对泛型成员执行任何赋值以外的操作,或者是调用System.Object中所没有的方法,就需要在类型参数上使用约束。
    无限制类型参数的一般用法
    没有约束的类型参数,如公有类MyClass{...}中的T, 被称为无限制类型参数(unbounded type parameters)。无限制类型参数有以下规则:
    l 不能使用运算符 != 和 == ,因为无法保证具体的类型参数能够支持这些运算符。
    l 它们可以与System.Object相互转换,也可显式地转换成任何接口类型。
    l 可以与null比较。如果一个无限制类型参数与null比较,当此类型参数为值类型时,比较的结果总为false。
    无类型约束
    当约束是一个泛型类型参数时,它就叫无类型约束(Naked type constraints)。当一个有类型参数成员方法,要把它的参数约束为其所在类的类型参数时,无类型约束很有用。如下例所示:
    class List
    {
    //...
    void Add(List items) where U:T {...}
    }

    在上面的示例中, Add方法的上下文中的T,就是一个无类型约束;而List类的上下文中的T,则是一个无限制类型参数。
    无类型约束也可以用在泛型类的定义中。注意,无类型约束一定也要和其它类型参数一起在尖括号中声明:
    //naked type constraint
    public class MyClass where T : V
    因为编译器只认为无类型约束是从System.Object继承而来,所以带有无类型约束的泛型类的用途十分有限。当你希望强制两个类型参数具有继承关系时,可对泛型类使用无类型约束。

    五、泛型类
    泛型类封装了不针对任何特定数据类型的操作。泛型类常用于容器类,如链表、哈希表、栈、队列、树等等。这些类中的操作,如对容器添加、删除元素,不论所存储的数据是何种类型,都执行几乎同样的操作。
    对大多数情况,推荐使用.NET框架2.0类库中所提供的容器类。有关使用这些类的详细信息,请参见基础类库中的泛型。
    通常,从一个已有的具体类来创建泛型类,并每次把一个类型改为类型参数,直至达到一般性和可用性的最佳平衡。当创建你自己的泛型类时,需要重点考虑的事项有:
    l 哪些类型应泛化为类型参数。一般的规律是,用参数表示的类型越多,代码的灵活性和复用性也就越大。过多的泛化会导致代码难以被其它的开发人员理解。
    l 如果有约束,那么类型参数需要什么样约束。一个良好的习惯是,尽可能使用最大的约束,同时保证可以处理所有需要处理的类型。例如,如果你知道你的泛型类只打算使用引用类型,那么就应用这个类的约束。这样可以防止无意中使用值类型,同时可以对T使用as运算符,并且检查空引用。
    l 把泛型行为放在基类中还是子类中。泛型类可以做基类。同样非泛型类的设计中也应考虑这一点。泛型基类的继承规则 。
    l 是否实现一个或多个泛型接口。例如,要设计一个在基于泛型的容器中创建元素的类,可能需要实现类似IComparable的接口,其中T是该类的参数。
    泛型概述中有一个简单泛型类的例子。
    类型参数和约束的规则对于泛型类的行为(behavior)有一些潜在的影响,——尤其是对于继承和成员可访问性。在说明这个问题前,理解一些术语十分重要。对于一个泛型类Node,客户代码既可以通过指定一个类型参数来创建一个封闭构造类型(Node),也可以保留类型参数未指定,例如指定一个泛型基类来创建开放构造类型(Node)。泛型类可以继承自具体类、封闭构造类型或开放构造类型:
    // concrete type
    class Node : BaseNode
    //closed constructed type
    class Node : BaseNode
    //open constructed type
    class Node : BaseNode
    非泛型的具体类可以继承自封闭构造基类,但不能继承自开放构造基类。这是因为客户代码无法提供基类所需的类型参数。
    //No error.
    class Node : BaseNode
    //Generates an error.
    class Node : BaseNode
    泛型的具体类可以继承自开放构造类型。除了与子类共用的类型参数外,必须为所有的类型参数指定类型,如下代码所示:
    //Generates an error.
    class Node : BaseNode {...}
    //Okay.
    class Node : BaseNode{...}
    继承自开放结构类型的泛型类,必须指定:
    Generic classes that inherit from open constructed types must specify must specify constraints that are a superset of, or imply, the constraints on the base type:
    class NodeItem where T : IComparable, new() {...}
    class MyNodeItem : NodeItem where T : IComparable , new(){...}

    泛型类型可以使用多种类型参数和约束,如下:
    class KeyType{...}
    class SuperKeyType where U : IComparable, where V : new(){...}
    开放结构和封闭构造类型型可以用作方法的参数:
    void Swap(List list1, List list2){...}
    void Swap(List list1, List list2){...}

    六、泛型接口
    不论是为泛型容器类,还是表示容器中元素的泛型类,定义接口是很有用的。把泛型接口与泛型类结合使用是更好的用法,比如用IComparable而非IComparable,以避免值类型上的装箱和拆箱操作。.NET框架2.0类库定义了几个新的泛型接口,以配合System.Collections.Generic中新容器类的使用。
    当一个接口被指定为类型参数的约束时,只有实现该接口的类型可被用作类型参数。下面的示例代码显示了一个从MyList派生的SortedList类。更多信息,请参见泛型概述。SortedList增加了约束where T : IComparable
    这使得SortedList中的BubbleSort方法可以使用表中的元素的IComparable.CompareTo方法。在这个例子中,表中的元素是简单类——实现IComparable 的Person类。
    using System;
    using System.Collections.Generic;
    //Type parameter T in angle brackets.
    public class MyList
    {
    protected Node head;
    protected Node current = null;
    // Nested type is also generic on T
    protected class Node
    {
    public Node next;
    //T as private member datatype.
    private T data;
    //T used in non-generic constructor.
    public Node(T t)
    {
    next = null;
    data = t;
    }
    public Node Next
    {
    get { return next; }
    set { next = value; }
    }
    //T as return type of property.
    public T Data
    {
    get { return data; }
    set { data = value; }
    }
    }
    public MyList()
    {
    head = null;
    }
    //T as method parameter type.
    public void AddHead(T t)
    {
    Node n = new Node(t);
    n.Next = head;
    head = n;
    }
    // Implement IEnumerator to enable foreach
    // iteration of our list. Note that in C# 2.0
    // you are not required to implment Current and
    // GetNext. The compiler does that for you.
    public IEnumerator GetEnumerator()
    {
    Node current = head;
    while (current != null)
    {
    yield return current.Data;
    current = current.Next;
    }
    }
    }
    public class SortedList : MyList where T : IComparable
    {
    // A simple, unoptimized sort algorithm that
    // orders list elements from lowest to highest:
    public void BubbleSort()
    {
    if (null == head || null == head.Next)
    return;
    bool swapped;
    do
    {
    Node previous = null;
    Node current = head;
    swapped = false;
    while (current.next != null)
    {
    // Because we need to call this method, the SortedList
    // class is constrained on IEnumerable
    if (current.Data.CompareTo(current.next.Data) > 0)
    {
    Node tmp = current.next;
    current.next = current.next.next;
    tmp.next = current;
    if (previous == null)
    {
    head = tmp;
    }
    else
    {
    previous.next = tmp;
    }
    previous = tmp;
    swapped = true;
    }
    else
    {
    previous = current;
    current = current.next;
    }
    }// end while
    } while (swapped);
    }
    }
    // A simple class that implements IComparable
    // using itself as the type argument. This is a
    // common design pattern in objects that are
    // stored in generic lists.
    public class Person : IComparable
    {
    string name;
    int age;
    public Person(string s, int i)
    {
    name = s;
    age = i;
    }
    // This will cause list elements
    // to be sorted on age values.
    public int CompareTo(Person p)
    {
    return age - p.age;
    }
    public override string ToString()
    {
    return name + ":" + age;
    }
    // Must implement Equals.
    public bool Equals(Person p)
    {
    return (this.age == p.age);
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    //Declare and instantiate a new generic SortedList class.
    //Person is the type argument.
    SortedList list = new SortedList ();
    //Create name and age values to initialize Person objects.
    string[] names = new string[]{"Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul"};
    int[] ages = new int[]{45, 19, 28, 23, 18, 9, 108, 72, 30, 35};
    //Populate the list.
    for (int x = 0; x < 10; x++)
    {
    list.AddHead(new Person(names[x], ages[x]));
    }
    //Print out unsorted list.
    foreach (Person p in list)
    {
    Console.WriteLine(p.ToString());
    }
    //Sort the list.
    list.BubbleSort();
    //Print out sorted list.
    foreach (Person p in list)
    {
    Console.WriteLine(p.ToString());
    }
    Console.WriteLine("Done");
    }
    }
    可以在一个类型指定多个接口作为约束,如下:
    class Stack where T : IComparable, IMyStack1{}
    一个接口可以定义多个类型参数,如下:

    IDictionary
    接口和类的继承规则相同:
    //Okay.
    IMyInterface : IBaseInterface
    //Okay.
    IMyInterface : IBaseInterface
    //Okay.
    IMyInterface: IBaseInterface
    //Error.
    IMyInterface : IBaseInterface2
    具体类可以实现封闭构造接口,如下:
    class MyClass : IBaseInterface
    泛型类可以实现泛型接口或封闭构造接口,只要类的参数列表提供了接口需要的所有参数,如下:
    //Okay.
    class MyClass : IBaseInterface
    //Okay.
    class MyClass : IBaseInterface

    泛型类、泛型结构,泛型接口都具有同样方法重载的规则。详细信息,请参见泛型方法。
  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/weblogical/p/1219547.html
Copyright © 2011-2022 走看看