コード例 #1
0
ファイル: Program.cs プロジェクト: jinshan1979/Algorithm
        static void Main(string[] args)
        {
            int      iValue  = 123;
            string   sValue  = "456";
            DateTime dtValue = DateTime.Now;

            Console.WriteLine("***********CommonMethod***************");
            CommonMethod.ShowInt(iValue);
            CommonMethod.ShowString(sValue);
            CommonMethod.ShowDateTime(dtValue);
            Console.WriteLine("***********Object***************");
            CommonMethod.ShowObject(iValue);
            CommonMethod.ShowObject(sValue);
            CommonMethod.ShowObject(dtValue);
            Console.WriteLine("***********Generic***************");
            GenericMethod.Show <int>(iValue);
            GenericMethod.Show <string>(sValue);
            GenericMethod.Show <DateTime>(dtValue);

            Console.WriteLine(typeof(List <>));
            Console.WriteLine(typeof(Dictionary <,>));
            Console.ReadKey();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: ewin66/CSharp-Study
        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();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            try
            {
                {
                    Console.WriteLine("*****************Old Way***********");
                    int      i  = 123;
                    string   s  = "456";
                    DateTime dt = DateTime.Now;
                    object   o  = "sorry";
                    {
                        Console.WriteLine("************************");
                        CommonMethod.ShowInt(i);
                        CommonMethod.ShowString(s);
                        CommonMethod.ShowDateTime(dt);
                        CommonMethod.ShowObject(o);
                        CommonMethod.ShowObject(i);
                        CommonMethod.ShowObject(s);
                    }


                    {
                        Console.WriteLine("*************Generic**************");
                        GenericMethod.Show(i);          // <> 能省略,自动推算
                        GenericMethod.Show <string>(s); //需要指定参数类型
                    }

                    {
                        Console.WriteLine("*************Monitor**************");
                        // 性能 common ~ Generic > object
                        Monitor.Show();

                        GenericCacheTest.Show();
                    }
                    {
                        Console.WriteLine("********************Constraint*************");
                        {
                            People people = new People()
                            {
                                Id   = 123,
                                Name = "people"
                            };

                            Chinese chinese = new Chinese()
                            {
                                Id   = 234,
                                Name = "Ivan"
                            };

                            Hubei hubei = new Hubei()
                            {
                                Id   = 345,
                                Name = "Hubei"
                            };

                            Japanese japanese = new Japanese()
                            {
                                Id   = 567,
                                Name = "Cang"
                            };

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

                            GenericMethod.Show <People>(people);
                            GenericMethod.Show <Chinese>(chinese);
                            GenericMethod.Show <Hubei>(hubei);
                            GenericMethod.Show <Japanese>(japanese);
                            {
                                Constraint.Show <People>(people);
                                Constraint.Show <Chinese>(chinese);
                                Constraint.Show <Hubei>(hubei);
                                //  Constraint.Show<Japanese>(japanese); Error because Japanese not a child class of People
                            }
                        }

                        {
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.Read();
        }
コード例 #4
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();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            try
            {
                int      iValue  = 123;
                string   sValue  = "456";
                DateTime dtValue = DateTime.Now;
                object   oValue  = new object();

                Console.WriteLine(typeof(List <int>));
                Console.WriteLine(typeof(Dictionary <string, int>));
                Console.WriteLine("*************************************");
                CommonMethod.ShowInt(iValue);
                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 <string>(sValue);
                GenericMethod.Show <DateTime>(dtValue);
                Console.WriteLine("*************************************");

                GenericConstraint.Show <People>(new People {
                    Id = 1001, Name = "lucy"
                });
                GenericConstraint.Show <Chinese>(new Chinese {
                    Id = 1002, Name = "Coco"
                });
                GenericConstraint.Show <JiangXi>(new JiangXi {
                    Id = 1003, Name = "Jack"
                });
                //GenericConstraint.Show<int>(1);//约束后,只能按约束传递

                //GenericConstraint.ShowInterface<People>(new People { Id = 1001, Name = "lucy" });
                GenericConstraint.ShowInterface <Chinese>(new Chinese {
                    Id = 1002, Name = "Coco"
                });
                GenericConstraint.ShowInterface <JiangXi>(new JiangXi {
                    Id = 1003, Name = "Jack"
                });
                GenericConstraint.ShowInterface <Japanese>(new Japanese {
                    Id = 1004, Name = "鬼子"
                });
                #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(oValue);//装箱,值类型是分配在栈上的,引用类型是分配在堆上的
                    }
                    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();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            int      iValue  = 123;
            string   sValue  = "abc";
            DateTime dtValue = DateTime.Now;

            Console.WriteLine("****************常规调用****************");
            {
                CommonMethod.ShowInt(iValue);
                CommonMethod.ShowString(sValue);
                CommonMethod.ShowDateTime(dtValue);
            }

            Console.WriteLine("****************object调用****************");
            {
                CommonMethod.ShowObject(iValue);
                CommonMethod.ShowObject(sValue);
                CommonMethod.ShowObject(dtValue);
            }

            Console.WriteLine("****************泛型调用****************");
            {
                GenericMethod.Show <int>(iValue);
                GenericMethod.Show(sValue); // 可以省略,自动推算
                GenericMethod.Show <DateTime>(dtValue);
            }

            Console.WriteLine("****************Monitor****************");
            {
                Monitor.Show(iValue);
            }

            Console.WriteLine("****************约束****************");
            {
                People people = new People
                {
                    Id   = 11,
                    Name = "张三"
                };
                Teacher teacher = new Teacher
                {
                    Id   = 12,
                    Name = "李老师"
                };
                Student student = new Student
                {
                    Id   = 13,
                    Name = "赵同学"
                };
                //Constraint.Show<People>(people);
                Constraint.Show <Teacher>(teacher);
                //Constraint.Show<Student>(student);
            }

            Console.WriteLine("****************协变&异变****************");
            {
                People people = new Teacher();
                //IList<People> peoples = new List<Teacher>();
                IEnumerable <People> peoples = new List <Teacher>();
            }

            Console.WriteLine("****************泛型缓存****************");
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(GenericCache <int> .GetCache());
                    Thread.Sleep(10);
                    Console.WriteLine(GenericCache <long> .GetCache());
                    Thread.Sleep(10);
                    Console.WriteLine(GenericCache <DateTime> .GetCache());
                    Thread.Sleep(10);
                    Console.WriteLine(GenericCache <string> .GetCache());
                    Thread.Sleep(10);
                }
            }

            Console.WriteLine("****************Monitor****************");
            {
                Monitor.Show();
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            try
            {
                // 欢迎大家来到.Net 高级班的Vip课程,我是Richard老师!
                // 今天是第十三期的第一次课:泛型
                //什么是泛型?
                //宽泛的,不确定的,就是不确定的类型
                //List<string> intlist = new List<string>();

                // var i = 1234; // 语法糖


                Console.WriteLine(typeof(List <>));
                Console.WriteLine(typeof(Dictionary <,>));

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

                Console.WriteLine("***********************Common***********************");
                CommonMethod.ShowInt(iValue);
                CommonMethod.ShowString(sValue);
                CommonMethod.ShowDateTime(dtValue);

                Console.WriteLine("***********************Object***********************");
                CommonMethod.ShowObject(oValue);
                CommonMethod.ShowObject(iValue);
                CommonMethod.ShowObject(sValue);
                CommonMethod.ShowObject(dtValue);

                Console.WriteLine("***********************Generic***********************");
                // 泛型只有:泛型方法、泛型类、泛型接口、泛型委托
                //泛型方法调用的时候,需要加上<>,而且需要指定具体的类型、指定的类型和传入的参数类型保持一致。

                CommonMethod.Show(iValue);                                      //如果类型参数,可以通过参数类型推导出来,那么就可以省略
                                           /* CommonMethod.Show<int>(sValue);*/ // 因为类型错了
                CommonMethod.Show(sValue);
                CommonMethod.Show <DateTime>(dtValue);
                CommonMethod.Show <object>(oValue);

                //Monitor.Show();

                Console.WriteLine("***********************GenericCache***********************");
                //GenericCacheTest.Show();

                //泛型方法  一个方法满足多个类型的需求
                //泛型类   就是一个类 满足多个类型的需求
                //泛型接口 就是一个接口 满足多个多个类型的需求
                //泛型委托 就是一个委托 满足多个多个类型的需求

                //GenericConstraint.ShowObject(iValue);
                //GenericConstraint.ShowObject(sValue);
                //GenericConstraint.ShowObject(dtValue);
                //GenericConstraint.ShowObject(oValue);

                Console.WriteLine("***********************GenericConstraint***********************");
                // 泛型约束的好处:
                // 加了约束以后可以获取更多的功能;
                // 程序在调用的时候可以避免错误调用
                People people = new People()
                {
                    Id   = 123,
                    Name = "Richard"
                };

                Chinese chinese = new Chinese()
                {
                    Id   = 234,
                    Name = "习大大"
                };
                Hubei hubei = new Hubei()
                {
                    Id   = 345,
                    Name = "王市长"
                };
                Japanese japanese = new Japanese()
                {
                    Id   = 678,
                    Name = "福原爱"
                };

                // Object 方法因为可以出入任何类型,没有限制,如果传入的类型不匹配,就会发生异常(类型安全问题)
                //GenericConstraint.ShowObject(people);
                //GenericConstraint.ShowObject(chinese);
                //GenericConstraint.ShowObject(hubei);
                //GenericConstraint.ShowObject(japanese);

                //GenericConstraint.Show(people);
                //GenericConstraint.Show(chinese);
                //GenericConstraint.Show(hubei);
                //GenericConstraint.Show(japanese);//


                // GenericConstraint.GenericShow(123);

                GenericConstraint.GenericShow(people);
                GenericConstraint.GenericShow(chinese);
                GenericConstraint.GenericShow(hubei);
                GenericConstraint.GenericShow(japanese);//
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }