Пример #1
0
        static void Main2(string[] args)
        {
            #region IDisposable
            Stopwatch sw = new Stopwatch();

            TestDisponsable test = new TestDisponsable();
            try
            {
                test.DoSomething();
            }
            finally
            {
                test.Dispose();
            }

            #endregion


            #region 泛型方法测试

            showInt(1);
            showObj(1);
            show <int>(1);

            string result = string.Empty;
            int    count  = 1;
            //测量一个时间间隔的运行时间  ElapsedMilliseconds 毫秒
            Stopwatch watch = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
            {
                showInt(1111);
            }
            watch.Stop();
            var t1 = watch.ElapsedMilliseconds;

            watch.Restart();
            for (int i = 0; i < count; i++)
            {
                showObj(1111);
            }
            watch.Stop();
            var t2 = watch.ElapsedMilliseconds;

            watch.Restart();
            for (int i = 0; i < count; i++)
            {
                show <int>(1111);
            }
            watch.Stop();
            var t3 = watch.ElapsedMilliseconds;

            Console.WriteLine($"{t1}-{t2}-{t3}");

            #endregion


            #region 泛型类测试

            BaseClas <int> baseInt = new BaseClas <int>();
            baseInt._T = 123;

            BaseClas <string> baseString = new BaseClas <string>();
            baseString._T = "sun";

            #endregion


            #region 泛型接口

            #endregion


            #region  约束泛型

            People people = new People()
            {
                Id   = 1,
                Name = "people"
            };
            Chinese cn = new Chinese()
            {
                Id   = 1,
                Name = "Chinese",
            };

            Anhui ah = new Anhui()
            {
                Id   = 1,
                Name = "anhui",
            };

            Japen japen = new Japen()
            {
                Id   = 1,
                Name = "japen"
            };

            //输出id 和 name
            //showObj2(people);
            //showObj2(cn);
            //showObj2(ah);
            //showObj2(japen); //会出错


            show2(people);
            show2(cn);
            show2(ah);
            //show2(japen);



            Animal animal = new Animal();
            animal.Name = "动物";
            Dog dog = new Dog();
            dog.Name = "狗";
            dog.age  = 2;
            Animal dog2 = new Dog();
            //List<Animal> list = new List<Dog>();

            //协变
            IEnumerable <Animal> list = new List <Dog>();

            Func <Animal> func = new Func <Dog>(() => null);

            IBaseClass2Out <Animal> list2 = new BaseClass2 <Dog>();
            Console.ReadKey();
            #endregion
        }