Exemple #1
0
 // 为啥不直接用基类: 约束可以叠加,更灵活
 // 此实现与 line 19 - line24效果一样
 // 但是使用泛型约束可以不止局限在People
 // 可以让它既是people 或 people 子类, 也可以同时
 // 是ISports, IWork
 public static void ShowBase(People peop)
 {
     Console.WriteLine($"{peop.Id}_ {peop.Name}");
     peop.Hi();
 }
Exemple #2
0
        static void Main(string[] args)
        {
            try
            {
                //var list = new List<int>() { 1, 2, 3, 4 }
                //.Select(i => new
                //{
                //    id = i,
                //    name = "Test" + i
                //});
                //foreach (var item in list)
                //{
                //    Console.WriteLine(item.id);
                //    Console.WriteLine(item.name);
                //}

                //List<int>


                Console.WriteLine("欢迎来到.net高级班vip课程,今天是Eleven老师给大家带来的泛型Generic");
                int      iValue  = 123;
                string   sValue  = "456";
                DateTime dtValue = DateTime.Now;
                object   oValue  = new object();



                Console.WriteLine(typeof(List <int>));
                Console.WriteLine(typeof(Dictionary <int, string>));
                Console.WriteLine("**************************");
                CommonMethod.ShowInt(iValue);
                //CommonMethod.ShowInt(sValue);
                CommonMethod.ShowString(sValue);
                CommonMethod.ShowDateTime(dtValue);

                Console.WriteLine("**************************");
                CommonMethod.ShowObject(oValue);

                CommonMethod.ShowObject(iValue);
                CommonMethod.ShowObject(sValue);
                CommonMethod.ShowObject(dtValue);

                Console.WriteLine("**************************");
                GenericMethod.Show <object>(oValue);
                GenericMethod.Show <int>(iValue);
                GenericMethod.Show(iValue);//类型参数可以省略,由编译器推断出来
                //GenericMethod.Show<int>(sValue);//类型参数和参数必须匹配
                GenericMethod.Show <string>(sValue);
                GenericMethod.Show <DateTime>(dtValue);

                Console.WriteLine("**************************");
                People people = new People()
                {
                    Id   = 11,
                    Name = "山冈"
                };
                Japanese japanese = new Japanese()
                {
                    Id   = 112,
                    Name = "鬼子"
                };

                Chinese chinese = new Chinese()
                {
                    Id   = 123,
                    Name = "口口"
                };
                Hubei hubei = new Hubei()
                {
                    Id   = 123,
                    Name = "pig猪"
                };


                //Constraint.Show<People>(people);
                Constraint.Show <Chinese>(chinese);
                Constraint.Show <Hubei>(hubei);

                Constraint.ShowPeople(people);
                Constraint.ShowPeople(chinese);
                Constraint.ShowPeople(hubei);


                //Constraint.ShowInterface<People>(people);//没有实现ISports接口
                Constraint.ShowInterface <Chinese>(chinese);
                Constraint.ShowInterface <Hubei>(hubei);

                Constraint.ShowInterface <Japanese>(japanese);

                //Constraint.Show<Japanese>(japanese);//虽然Japanese有ID和Name,但是因为不是People,所以不能调用

                //Constraint.Show<int>(iValue);//约束后,只能按约束传递

                #region Monitor
                {
                    long commonTime  = 0;
                    long objectTime  = 0;
                    long genericTime = 0;
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();

                        for (int i = 0; i < 1000000000; i++)
                        {
                            ShowCommon(iValue);
                        }
                        stopwatch.Stop();
                        commonTime = stopwatch.ElapsedMilliseconds;
                    }
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();

                        for (int i = 0; i < 1000000000; i++)
                        {
                            ShowObject(iValue);
                        }
                        stopwatch.Stop();
                        objectTime = stopwatch.ElapsedMilliseconds;
                    }
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();

                        for (int i = 0; i < 1000000000; i++)
                        {
                            ShowGeneric <int>(iValue);
                        }
                        stopwatch.Stop();
                        genericTime = stopwatch.ElapsedMilliseconds;
                    }
                    Console.WriteLine("commonTime = {0} objectTime = {1} genericTime = {2}", commonTime, objectTime, genericTime);
                }



                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Exemple #3
0
 public static void ShowPeople(People PParameter)
 {
     Console.WriteLine("这里是Common,ShowInt,parameter={0},parameter={1}", PParameter, PParameter.GetType());
 }
Exemple #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="people"></param>
 public static void ShowBase(People people)
 {
     Console.WriteLine($"{people.Id}-{people.Name}");
 }
Exemple #5
0
        static void Main(string[] args)
        {
            Console.WriteLine(".net framework 1.0的时候");
            int      intValue    = 1;
            string   stringValue = "1";
            DateTime dateValue   = DateTime.Now;

            CommonMethod.ShowInt(intValue);
            CommonMethod.ShowString(stringValue);
            CommonMethod.ShowDateTime(dateValue);
            Console.WriteLine("使用object简化代码");
            CommonMethod.ShowObj(intValue);
            CommonMethod.ShowObj(stringValue);
            CommonMethod.ShowObj(dateValue);

            Console.WriteLine(".net framework 2.0的时候,引入泛型");
            GenericMethod.Show(intValue);
            GenericMethod.Show(stringValue);
            GenericMethod.Show(dateValue);

            Console.WriteLine("打印泛型类型,会看到编译器,生成占位符");
            Console.WriteLine(typeof(List <>));
            Console.WriteLine(typeof(Dictionary <,>));

            Console.WriteLine("比较性能");
            Monitor.Show();

            // 泛型类

            // 泛型接口

            // 泛型约束
            var people = new People()
            {
                Id   = 1,
                Name = "人类"
            };

            var chinese = new Chinese()
            {
                Id   = 2,
                Name = "中国人"
            };
            var japanese = new Japanese()
            {
                Id   = 3,
                Name = "日本人"
            };

            CommonMethod.ShowObj(people);
            CommonMethod.ShowObj(chinese);
            CommonMethod.ShowObj(japanese);

            GenericMethod.Show(people);
            GenericMethod.Show(chinese);
            GenericMethod.Show(japanese);

            // 上面方法只是打印了对象的类型
            // 如果想打印 对象里的内容 输出Id,Name的值

            Constraint.ShowBase(people);
            Constraint.Show(chinese);
            //Constraint.Show(japanese); 由于japannese 不是继承自 People 类所以这里不能使用

            // 为啥不直接使用基类 方法

            // 泛型类缓存
            GenericCacheTest.Show();
        }
 public static void ShowBase(People tParameter)//Constraint can be overlap,
 {
     Console.WriteLine($"{tParameter.Id}_{tParameter.Name}");
     tParameter.Hello();
 }
Exemple #7
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班vip课程,今天是Eleven老师给大家带来的泛型Generic");
                //List<int>
                //List<string>

                //Console.WriteLine(typeof(List<>));//`1
                //Console.WriteLine(typeof(Dictionary<,>));//`2
                //var varValue = 789;
                //object objectValue = 789;
                //int iValue = 123;
                //dynamic dValue = 789;

                ////int result = varValue + iValue;
                ////int result2 = objectValue + iValue;
                //int result3 = dValue + iValue;
                //string sValue = "456";
                //DateTime dtValue = DateTime.Now;
                //object oValue = "MrSorry";

                //Console.WriteLine("**************************");
                //CommonMethod.ShowInt(iValue);
                //CommonMethod.ShowString(sValue);
                //CommonMethod.ShowDateTime(dtValue);
                //CommonMethod.ShowObject(oValue);

                //CommonMethod.ShowObject(iValue);
                //CommonMethod.ShowObject(sValue);
                //CommonMethod.ShowObject(dtValue);

                //Console.WriteLine("************Generic**************");
                //GenericMethod.Show<int>(iValue);//需要指定类型参数
                ////GenericMethod.Show<string>(iValue);//必须吻合
                //GenericMethod.Show(iValue);//能省略,自动推算
                //GenericMethod.Show<string>(sValue);
                //GenericMethod.Show<DateTime>(dtValue);
                //GenericMethod.Show<object>(oValue);


                ////GenericClass<int> genericClass = new GenericClass<int>()
                ////{
                ////    _T = 123
                ////};
                ////GenericClass<string> genericClassString = new GenericClass<string>()
                ////{
                ////    _T = "123"
                ////};

                //Console.WriteLine("************Monitor**************");
                //Monitor.Show();
                ////generic≈≈common>object
                ////泛型:又叫马儿跑,又叫马儿不吃草! 因为框架的升级

                GenericCacheTest.Show();

                Console.WriteLine("************Constraint*****************");
                {
                    People people = new People()
                    {
                        Id   = 123,
                        Name = "走自己的路"
                    };
                    Chinese chinese = new Chinese()
                    {
                        Id   = 234,
                        Name = "晴天"
                    };
                    Hubei hubei = new Hubei(123)
                    {
                        Id   = 345,
                        Name = "流年"
                    };
                    Japanese japanese = new Japanese()
                    {
                        Id   = 7654,
                        Name = "苍老师"//寒露的建议
                    };

                    //CommonMethod.ShowObject(people);
                    //CommonMethod.ShowObject(chinese);
                    //CommonMethod.ShowObject(hubei);
                    //CommonMethod.ShowObject(japanese);

                    //Constraint.Show<People>(people);
                    Constraint.Show <Chinese>(chinese);
                    //Constraint.Show<Hubei>(hubei);
                    //Constraint.Show<Japanese>(japanese);//
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }