static void Main(string[] args) { try { Console.WriteLine("**********************Lambda + Linq*************************"); { Console.WriteLine("**************************Lambda*********************"); LambdaShow lambdaShow = new LambdaShow(); lambdaShow.Show(); } { //匿名类 object model = new { id = 2, Name = "", Age = 25, ClassId = 2 }; // Console.WriteLine(model.id); //object 编译器不允许 dynamic dModel = new // 4.0 { id = 2, Name = "", Age = 25, ClassId = 2 }; Console.WriteLine(dModel.id); // var 是一个语法糖, 由编译器自动推算 // var 必须在声明时就确定类型 // var 确定类型后不能改变的 // var vModel = new //3.0 编译后是有一个真实的类 { id = 2, Name = "", Age = 25, ClassId = 2 }; //vModel.id = 3; //匿名类里面的属性只能Get不能被赋值, 只能在构造函数指定 Console.WriteLine(vModel.id); } { //扩展方法 3.0 想要增加方法但是又不想改变原有的类 Student student = new Student() { Id = 2, Name = "Ivan", Age = 25, ClassId = 2 }; student.Study(); student.Sing(); } } catch (Exception e) { Console.WriteLine(e.Message); //throw; } Console.ReadKey(); }
static void Main(string[] args) { { Console.WriteLine("***************Lambda*****************"); LambdaShow lambdaShow = new LambdaShow(); lambdaShow.Show(); } { object o1 = new { Id = 3, age = 25 }; // o1.Id //not work dynamic o2 = new { Id = 3, age = 25 }; Console.WriteLine(o2.Id); //dynamic could ignore the compiler checking. var o3 = new { Id = 3, age = 25 }; Console.WriteLine(o3.Id); //dynamic could ignore the compiler checking. } #region ExtendMethod { Student student = new Student() { Id = 1, Name = "Jason", Age = 25, ClassId = 2 }; string s = null; student.Study(); student.StudyHard(); //又要增加方法 又不想(不能)修改 student.Sing(); ExtendMethod.Sing(student); int?iValue = null; int r1 = iValue.ToInt(0); int i1 = 2, i2 = 4; bool r2 = i1.greaterThan(i2); int r3 = student.Length(); } #endregion #region linq { Console.WriteLine("***************Linq*****************"); LinqShow show = new LinqShow(); show.Show(); } #endregion Console.Read(); }