Code using System; namespace net30netfeature { publicclass ObjectInit { /**//* * 对象初使化器包括了一个用逗号分隔的值列表。用{ and }包起来。被初使化的每个成员是公共的属性或字段。 * 在使用对象初始化器时,默认的构造函数被调用,然后在其后对相应的公共成员进行赋值。对象初始化器这个特性 * 实质上是在语法上进行了简化。生成的最终的IL还是一个调用默认的构造函数,然后在其后对相应的公共成员进行赋值 * 的过程 */ publicstaticvoid Test() { Test1(); } publicstaticvoid Test1() { // Make a Point by setting each property manually. Point firstPoint =new Point(); firstPoint.X =10; firstPoint.Y =10; // Make a Point via a custom constructor. Point anotherPoint =new Point(20, 20); // Make some Point types using the new object init syntax. //Under C# 3.0, we could now make Points using any of the following approaches: var yetAnotherPoint =new Point { X =30, Y =30 };//is implicitly typed Point finalPoint =new Point { X =30, Y =30 };//隐示的调用默认的构造函数。 Point finalPoint1 =new Point() { X =30, Y =30 };//显示的调用默认的构造函数。 /**//* * 导致X value of 100 and a Y value of 100, regardless of the fact that our constructor arguments * specified the values 10 and 16 */ Point pt =new Point(10, 16) { X =100, Y =100 }; Console.WriteLine(firstPoint.ToString()); Console.WriteLine(anotherPoint.ToString()); Console.WriteLine(yetAnotherPoint.ToString()); Console.WriteLine(finalPoint.ToString()); Console.WriteLine(finalPoint1.ToString()); Console.WriteLine(pt.ToString()); } } publicenum PointColor { LightBlue, BloodRed, Gold } publicstruct Point { publicint xPos, yPos; private PointColor c; public Point(PointColor color) { xPos =0; yPos =0; c = color; } public Point(int x, int y) { xPos = x; yPos = y; c = PointColor.Gold; } publicint X { get{ return xPos; } set{ xPos = value; } } publicint Y { get{ return yPos; } set{ yPos = value; } } //public override string ToString() //{ return string.Format("[{0}, {1}]", xPos, yPos); } publicoverridestring ToString() { returnstring.Format("[{0}, {1}, Color = {2}]", xPos, yPos, c); } } publicclass TestFalgEnum { publicstaticvoid TestEnumFlag() { //1248 //int i = 1; //int ii = 1; //Console.WriteLine(i ^ ii); test aa =new test(); test bb =new test(); aa = test.a; bb = test.b; test cc = aa | bb; Console.WriteLine(Enum.Format(typeof(test), cc, "d")); Console.WriteLine(cc == test.c); } } [Flags] enum test { a =1, b =2, c =3, d =8 } }