Exemplo n.º 1
0
        public A3(U u, V v)
        {
            A1 a1 = new A1();
            A2 a2 = new A2();

            a1.log();
            a2.log();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //c#中的可空类型
            int a = 0;
            int?b = null;

            Console.WriteLine("a = {0}, b = {1}", a, b);

            //泛型demo1
            var num = new NumberCollection();

            Console.WriteLine("====输出所有元素=======");
            num.Print();

            //自定义排序
            num.Sort(NumberCollectionDelegate.CompareDelegate);
            Console.WriteLine("=========自定义排序=========");
            num.Print();

            //自定义谓词 查找
            var num1 = new NumberCollection(num.FindAll(NumberCollectionDelegate.Predicate));

            Console.WriteLine("========自定义谓词查找=========");
            num1.Print();

            Dictionary <int, string> paras = new Dictionary <int, string>();

            paras.Add(0, "0000");
            paras.Add(1, "1111");
            paras.Add(2, "2222");

            Console.WriteLine("输出key value");
            foreach (KeyValuePair <int, string> kv in paras)
            {
                Console.WriteLine("key = {0}, value = {1}", kv.Key, kv.Value);
            }

            //基类约束
            A1          a1 = new A1();
            A2          a2 = new A2();
            A3 <A1, A2> a3 = new A3 <A1, A2>(a1, a2);

            //接口约束
            UU          u  = new UU();
            VV          v  = new VV();
            BB <UU, VV> bb = new BB <UU, VV>(u, v);

            //构造器约束 两种情况都是走不带参数的构造器

            Console.WriteLine("===构造器约束不带参数的情况====");
            C3 <C1> c311 = new C3 <C1>();
            C3 <C2> c321 = new C3 <C2>();

            Console.WriteLine("====构造器约束带参数的情况====");
            C3 <C1> c31 = new C3 <C1>("c1");
            C3 <C2> c32 = new C3 <C2>("c2");


            //泛型约束的值约束与引用约束
            D3 <int> d1 = new D3 <int>();

            //报错:u不是值类型
            //D3<u> d2 = new D3<u>();

            D4 <C2> d4 = new D4 <C2>();

            Console.Read();
        }