象初始化器(Object Initializers) 和 集合初始化器(Collection Initializers) ,就是简化我们的代码,让本来几行才能写完的代码一行写完。这样在LINQ的使用中,我们才不会把一个LINQ表达式写的巨复杂无比。
一、实例
使用对象初始化器
注意,在使用对象初始化器时,隐式的调用了默认的构造函数。
可以一次为多个(不一定要全部)属性赋值,
赋值的顺序是从左到右
所以,最后 xPos = 900;
相当于
一、实例
public enum PointColor
{ LightBlue, BloodRed, Gold }
public class Point
{
public int xPos, yPos;
private PointColor c;
public Point(PointColor color)
{
xPos = 0; yPos = 0;
c = color;
}
public Point(){}
public Point(int x, int y)
{
xPos = x; yPos = y;
c = PointColor.Gold;
}
public override string ToString()
{ return string.Format("[{0}, {1}, Color = {2}]", xPos, yPos, c); }
}
{ LightBlue, BloodRed, Gold }
public class Point
{
public int xPos, yPos;
private PointColor c;
public Point(PointColor color)
{
xPos = 0; yPos = 0;
c = color;
}
public Point(){}
public Point(int x, int y)
{
xPos = x; yPos = y;
c = PointColor.Gold;
}
public override string ToString()
{ return string.Format("[{0}, {1}, Color = {2}]", xPos, yPos, c); }
}
使用对象初始化器
Under C# 2008, we could now make Points using any of the following approaches:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Object Init Syntax *****\n");
// Make a Point by setting each property manually
Point firstPoint = new Point();
firstPoint.X = 10;
firstPoint.Y = 10;
// or make a Point via a custom constructor
Point anotherPoint = new Point(20, 20);
// or make some Point types using the new object init syntax.
var yetAnotherPoint = new Point { X = 30, Y = 30 };
Point finalPoint = new Point { X = 30, Y = 30 };
Console.ReadLine();
}
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Object Init Syntax *****\n");
// Make a Point by setting each property manually
Point firstPoint = new Point();
firstPoint.X = 10;
firstPoint.Y = 10;
// or make a Point via a custom constructor
Point anotherPoint = new Point(20, 20);
// or make some Point types using the new object init syntax.
var yetAnotherPoint = new Point { X = 30, Y = 30 };
Point finalPoint = new Point { X = 30, Y = 30 };
Console.ReadLine();
}
注意,在使用对象初始化器时,隐式的调用了默认的构造函数。
可以一次为多个(不一定要全部)属性赋值,
var p = new Point {xPos = 2, yPos = 3};
赋值的顺序是从左到右
var p = new Point {xPos = 2, yPos = 3, X = 900};
所以,最后 xPos = 900;
相当于
Point p = new Point();
p.xPos = 2;
p.yPos = 3;
p.X = 900;
p.xPos = 2;
p.yPos = 3;
p.X = 900;
二、在对象初始化器时调用自定义的构造函数
使用对象初始化器时,隐式的调用了默认的构造函数。也可以指定调用任何自定义的构造函数。
// Calling a custom constructor.
Point pt = new Point(10, 16) { X = 100, Y = 100 };
Point pt = new Point(10, 16) { X = 100, Y = 100 };
// Calling a more interesting custom constructor with init syntax.
Point goldPoint = new Point(PointColor.Gold){ X = 90, Y = 20 };
Console.WriteLine("Value of Point is: {0}", goldPoint);
Point goldPoint = new Point(PointColor.Gold){ X = 90, Y = 20 };
Console.WriteLine("Value of Point is: {0}", goldPoint);
三、初始化内部对象
public class Rectangle
{
private Point topLeft = new Point();
private Point bottomRight = new Point();
public Point TopLeft
{
get { return topLeft; }
set { topLeft = value; }
}
public Point BottomRight
{
get { return bottomRight; }
set { bottomRight = value; }
}
public override string ToString()
{
return string.Format("[TopLeft: {0}, {1}, BottomRight: {2}, {3}]", topLeft.X,
topLeft.Y, bottomRight.X, bottomRight.Y);
}
}
{
private Point topLeft = new Point();
private Point bottomRight = new Point();
public Point TopLeft
{
get { return topLeft; }
set { topLeft = value; }
}
public Point BottomRight
{
get { return bottomRight; }
set { bottomRight = value; }
}
public override string ToString()
{
return string.Format("[TopLeft: {0}, {1}, BottomRight: {2}, {3}]", topLeft.X,
topLeft.Y, bottomRight.X, bottomRight.Y);
}
}
使用对象初始化器,可以减少很多代码
// Create and initialize a Rectangle.
Rectangle myRect = new Rectangle
{
TopLeft = new Point { X = 10, Y = 10 },
BottomRight = new Point { X = 200, Y = 200}
};
Rectangle myRect = new Rectangle
{
TopLeft = new Point { X = 10, Y = 10 },
BottomRight = new Point { X = 200, Y = 200}
};
四、集合初始化器
以前的集合初始化如下:
// Init a standard array.
int[] myArrayOfInts = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init a generic List<> of ints.
List<int> myGenericList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init an ArrayList with numerical data.
ArrayList myList = new ArrayList { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] myArrayOfInts = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init a generic List<> of ints.
List<int> myGenericList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init an ArrayList with numerical data.
ArrayList myList = new ArrayList { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
使用集合初始化器,如下:
List<Point> myListOfPoints = new List<Point>
{
new Point { X = 2, Y = 2 },
new Point { X = 3, Y = 3 },
CHAPTER 13 n C# 2008 LANGUAGE FEATURES 439
new Point(PointColor.BloodRed){ X = 4, Y = 4 }
};
foreach (var pt in myListOfPoints)
{
Console.WriteLine(pt);
}
{
new Point { X = 2, Y = 2 },
new Point { X = 3, Y = 3 },
CHAPTER 13 n C# 2008 LANGUAGE FEATURES 439
new Point(PointColor.BloodRed){ X = 4, Y = 4 }
};
foreach (var pt in myListOfPoints)
{
Console.WriteLine(pt);
}
List<Rectangle> myListOfRects = new List<Rectangle>
{
new Rectangle {TopLeft = new Point { X = 10, Y = 10 },
BottomRight = new Point { X = 200, Y = 200}},
new Rectangle {TopLeft = new Point { X = 2, Y = 2 },
BottomRight = new Point { X = 100, Y = 100}},
new Rectangle {TopLeft = new Point { X = 5, Y = 5 },
BottomRight = new Point { X = 90, Y = 75}}
};
foreach (var r in myListOfRects)
{
Console.WriteLine(r);
}
{
new Rectangle {TopLeft = new Point { X = 10, Y = 10 },
BottomRight = new Point { X = 200, Y = 200}},
new Rectangle {TopLeft = new Point { X = 2, Y = 2 },
BottomRight = new Point { X = 100, Y = 100}},
new Rectangle {TopLeft = new Point { X = 5, Y = 5 },
BottomRight = new Point { X = 90, Y = 75}}
};
foreach (var r in myListOfRects)
{
Console.WriteLine(r);
}