public class A<T>{}
这啥意思啊,谁能给个例子效习效习

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace BCLTest
    {
        public static class Singleton<T>
        {
            private static T instance;        public static T NewInstance()
            {
                if (instance == null)
                {
                    instance = (T)Activator.CreateInstance(typeof(T), true);
                }            return instance;
            }
        }    public class Class1
        {
            private Class1() { }
        }    public class Class2
        {
            private Class2() { }
        }}
      

  2.   

    Class2 c2=Singleton<Class2>.NewInstance();
      

  3.   

    public class A <T>{
    public static T Add(float A1,float A2){
    return (T)(A1+A2);
    }
    } A<int>.Add(3.1,3.2)
    将返回int型
    A<float>.Add(3.1,3.2)
    将返回float型一个泛型就顶替了两个类:
    public class A {
    public static int Add(float A1,float A2){
    return (int)(A1+A2);
    }
    }

    public class A {
    public static float Add(float A1,float A2){
    return (float)(A1+A2);
    }
    }泛型是弱类型,就是在运行时才知道具体的类型,就容易出错。比如:
    A<float>.Add("1212",3.2)
    编译不出错,但运算执行的时候就出错了。
      

  4.   

    这是一个泛型类的定义,这是2.0新增的一个强大的功能,通过参数的类型化可以使同一段代码操作不同的参数类型.使用泛型可有带来很多的好处,如你可能仅仅因为参数类型的不同需定义重载的方法,或使用Objec参数,但是object会涉及到装箱拆箱的操作,不仅降低性能而且由于任何类型都可以和object发生强类型转换造成了类型不安全.泛型很好的解决了这些问题.
    // 尖括号中的类型参数 T。
        public class MyList<T> : IEnumerable<T>
        {
            protected Node head;
            protected Node current = null;        // 嵌套类型也是 T 上的泛型
            protected class Node
            {
                public Node next;
                // T 作为私有成员数据类型。
                private T data;
                // 在非泛型构造函数中使用的 T。
                public Node(T t)
                {
                    next = null;
                    data = t;
                }
                public Node Next
                {
                    get { return next; }
                    set { next = value; }
                }
                // T 作为属性的返回类型。
                public T Data
                {
                    get { return data; }
                    set { data = value; }
                }
            }        public MyList()
            {
                head = null;
            }        // T 作为方法参数类型。
            public void AddHead(T t)
            {
                Node n = new Node(t);
                n.Next = head;
                head = n;
            }        // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
            // foreach 迭代。请注意,在 C# 2.0 中, 
            // 不需要实现 Current 和 MoveNext。
            // 编译器将创建实现 IEnumerator<T> 的类。
            public IEnumerator<T> GetEnumerator()
            {
                Node current = head;            while (current != null)
                {
                    yield return current.Data;
                    current = current.Next;
                }
            }        // 必须实现此方法,因为
            // IEnumerable<T> 继承 IEnumerable
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }