详解C#中的泛型以及编程中使用泛型的优点

2019-12-26 17:33:37丽君

在 Node 嵌套类中作为公共方法 GetNext 和 Data 属性的返回类型。
在嵌套类中作为私有成员数据的类型。
注意,T 可用于 Node 嵌套类。如果使用具体类型实例化 GenericList<T>(例如,作为 GenericList<int>),则所有的 T 都将被替换为 int。


// type parameter T in angle brackets
public class GenericList<T> 
{
  // The nested class is also generic on T.
  private class Node
  {
    // T used in non-generic constructor.
    public Node(T t)
    {
      next = null;
      data = t;
    }

    private Node next;
    public Node Next
    {
      get { return next; }
      set { next = value; }
    }

    // T as private member data type.
    private T data;

    // T as return type of property.
    public T Data 
    {
      get { return data; }
      set { data = value; }
    }
  }

  private Node head;

  // constructor
  public GenericList() 
  {
    head = null;
  }

  // T as method parameter type:
  public void AddHead(T t) 
  {
    Node n = new Node(t);
    n.Next = head;
    head = n;
  }

  public IEnumerator<T> GetEnumerator()
  {
    Node current = head;

    while (current != null)
    {
      yield return current.Data;
      current = current.Next;
    }
  }
}

下面的代码示例演示客户端代码如何使用泛型 GenericList<T> 类来创建整数列表。只需更改类型参数,即可方便地修改下面的代码示例,创建字符串或任何其他自定义类型的列表:


class TestGenericList
{
  static void Main()
  {
    // int is the type argument
    GenericList<int> list = new GenericList<int>();

    for (int x = 0; x < 10; x++)
    {
      list.AddHead(x);
    }

    foreach (int i in list)
    {
      System.Console.Write(i + " ");
    }
    System.Console.WriteLine("nDone");
  }
}

泛型的优点
在公共语言运行时和 C# 语言的早期版本中,通用化是通过在类型与通用基类型 Object 之间进行强制转换来实现的,泛型提供了针对这种限制的解决方案。通过创建泛型类,您可以创建一个在编译时类型安全的集合。
使用非泛型集合类的限制可以通过编写一小段程序来演示,该程序使用 .NET Framework 类库中的 ArrayList 集合类。 ArrayList 是一个使用起来非常方便的集合类,无需进行修改即可用来存储任何引用或值类型。


// The .NET Framework 1.1 way to create a list:
System.Collections.ArrayList list1 = new System.Collections.ArrayList();
list1.Add(3);
list1.Add(105);

System.Collections.ArrayList list2 = new System.Collections.ArrayList();
list2.Add("It is raining in Redmond.");
list2.Add("It is snowing in the mountains.");

但这种方便是需要付出代价的。添加到 ArrayList 中的任何引用或值类型都将隐式地向上强制转换为 Object。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。