泛型(转)

原文地址:http://www.cnblogs.com/wisdomforce/archive/2010/05/30/1747426.html

1.泛型的概念

  • 泛型可将具体类型的指定推迟到客户端代码声明并实例化类的时候。
  • 使用泛型类型可以组嗲限度的重用代码、保护类型的安全以及提高程序的性能。
  • 泛型最常见的用途是创建类集合。
  • NET Framework类库在System.Collections.Generic命名空间中包含几个泛型集合类。应尽可能的使用这些类来代替普通的类,如System.Collections命名空间中的ArrayList等集合类。

2.泛型的优点

       通过创建泛型类,可以创建一个编译时类型安全的集合(编译时进行类型检查)。

   System.Collections.ArrayList list=new System.Collections.ArrayList();

   list.Add(3);

   list.Add("Hello,girl!");

   int t=0;

    foreach(int i in lis)

    {

           t+=i;

    }

如上代码,尽管将字符串和int组合在一个ArrayList中的做法在创建异类集合是完全合法的,因为ArrayList将把所有项强制转化为Object,但这种做法更能产生编译错误,并且运行时才能检测到错误。

    通过创建泛型类,可以提高程序的性能,避免不必要的强制类型转换、装箱、拆箱操作。

System.Collectons.ArrayList list=new System.Collections.ArrayList();

list.add("1");

list.add("2");

 

System.Collections.ArrayList list=new System.Collections.ArrayList();

list1.add("hello!");

list1.add("hello!");

上例代码说明ArrayList是一个使用起来非常方便的集合类,无需进行修改即可以用来存储任何引用或值类类型。但这种方法是需要付出代价的。添加到ArrayList中的任何引用或值类型都将隐式地向上强制转换为Object.如果项是值类型则必须进行装箱操作,在检索时,进行拆箱操作。强制转换及装箱和拆箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和拆箱的影响非常明显。

代码

 public class GenericList<T> //T为泛型的类型参数,但在客户端代码中指定T的具体类型时,泛型中的所有T都替换为具体的类型
    {
        //定义一个嵌套类
        private class Node
        {
            public Node(T t)
            {
                next = null;
                data = t;
            }
            private Node next;

            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            private T data;
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
        }
        private Node head;//声明一个嵌套类的变量

        public GenericList()
        {
            head = null;
           
        }

        //泛型类的添加成员方法
        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;
            }
        }
      

    }

4. 泛型和数组

在 C# 2.0 中,下限为零的一维数组自动实现 IList<T>。这使您可以创建能够使用相同代码循环访问数组和其他集合类型的泛型方法。如下示例

 

复制代码
class Program
{
static void Main()
{
int[] arr = { 0, 1, 2, 3, 4 };
List
<int> list = new List<int>();

for (int x = 5; x < 10; x++)
{
list.Add(x);
}

ProcessItems
<int>(arr);
ProcessItems
<int>(list);
}

static void ProcessItems<T>(IList<T> coll)
{
foreach (T item in coll)
{
System.Console.Write(item.ToString()
+ " ");
}
System.Console.WriteLine();
}
}

 

原文地址:https://www.cnblogs.com/lovezhangyu/p/3381843.html