c# object var dynamic简单比较

------------恢复内容开始------------

                Object Student = new
                {
                    Id = "1",
                    Name = "张三"
                };
                //Console.WriteLine(Student.Id);
                //Console.WriteLine(Student, Name); 
                //C#强类型语言,编译时会确定类型,object 决定了没有Id属性  
                //运行时确实有Id和Name  但是编译器不认可
            }
            {
                //dynamic会避开编译器检查,
                dynamic Student = new
                {
                    Id = "1",
                    Name = "张三"
                };
                //Console.WriteLine(Student.Id);
                //Console.WriteLine(Student.Name);
            }
            {
                //1 var 配合匿名类型使用,会根据右侧推断出类型
                //2 var 偷懒,复杂类型的使用
                var Student = new
                {
                    Id = "1",
                    Name = "张三"
                };
                Console.WriteLine(Student.Id);
                Console.WriteLine(Student.Name);
                //Student.Id = 3;//只读  只有初始化的时候指定
            }
            Console.ReadLine();

 

------------恢复内容结束------------

posted @ 2019-11-09 14:44  王大师123  阅读(351)  评论(0编辑  收藏  举报