An object creation expression (§7.5.10.1) may include an object or collection initializer which initializes the members of the newly created object or the elements of the newly created collection.
某个对象建立表达式(§7.5.10.1)可能包括一个对象或集合构造者,正是它们初始化新建立的对象成员或新建立的集合成员。
object-creation-expression:
new type ( argument-listopt ) object-or-collection-initializeropt
new type object-or-collection-initializer
object-or-collection-initializer:
object-initializer
collection-initializer
An object creation expression can omit the constructor argument list and enclosing parentheses provided it includes an object or collection initializer. Omitting the constructor argument list and enclosing parentheses is equivalent to specifying an empty argument list.
某个对象建立表达式能够删除构造函数的参数列表和圆括号,以提供包含的对象或集合的构造者。删除构造函数参数列表和圆括号相当于明确一个空的参数列表。
Execution of an object creation expression that includes an object or collection initializer consists of first invoking the instance constructor and then performing the member or element initializations specified by the object or collection initializer.
对象建立表达式的执行包括某个对象或集合构造者,并由第一个调用实例构造函数组成,并且,接着执行成员或元素初始化,这是由对象或集合构造者特定化的。
It is not possible for an object or collection initializer to refer to the object instance being initialized.
对于某个对象或集合的构造者而言,引用该对象实例进行初始化是不可能的。
An object initializer specifies values for one or more fields or properties of an object.
对象构造者特指对于某个对象的某一或几个field或属性值而言。
object-initializer:
{ member-initializer-listopt }
{ member-initializer-list , }
member-initializer-list:
member-initializer
member-initializer-list , member-initializer
member-initializer:
identifier = initializer-value
initializer-value:
expression
object-or-collection-initializer
An object initializer consists of a sequence of member initializers, enclosed by { and } tokens and separated by commas. Each member initializer must name an accessible field or property of the object being initialized, followed by an equals sign and an expression or an object or collection initializer. It is an error for an object initializer to include more than one member initializer for the same field or property.
对象构造者由一系列成员构造者组成,是由{和}标示的并由逗号分隔的。每个成员构造者必须命名一个准入的对象的field或属性,当该对象被初始化的时候,紧跟着通过等号付值以及某个表达式或某个对象或集合的构造者。对于某个对象构造者,想要包含多于一个的对象构造者,却对于同一个field或属性而言,那是错误的。
A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§7.13.1) to the field or property.
成员构造者特指某个等号后面的表达式,等同于对于field或属性的付值分配。
A member initializer that specifes an object initializer after the equals sign is an initialization of an embedded object. Instead of assigning a new value to the field or property, the assignments in the object initializer are treated as assignments to members of the field or property. A property of a value type cannot be initialized using this construct.
成员构造者特指某个等号后面的对象构造者,是内嵌对象的初始化。替代对于field或属性分配新值,对象构造者的付值视同对于field或属性的对象付值。值类型的属性不能够通过这种构造进行初始化。
A member initializer that specifies a collection initializer after the equals sign is an initialization of an embedded collection. Instead of assigning a new collection to the field or property, the elements given in the initializer are added to the collection referenced by the field or property. The field or property must be of a collection type that satisfies the requirements specified in §26.4.2.
成员构造者特指等号后面的集合构造者是,内嵌集合的初始化。替代对于field或属性分配新值,构造者所给予的元素,通过field或属性添加至集合的引用。Field或属性必须为集合类型,且满足于在§26.4.2中特指的需求。
The following class represents a point with two coordinates:
以下类代表一个点
public class Point
{
int x, y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
An instance of Point can be created an initialized as follows:
Point类的实例能够如下初始化建立:
var a = new Point { X = 0, Y = 1 };
which has the same effect as
这和下面的影响是等同的
var a = new Point();
a.X = 0;
a.Y = 1;
The following class represents a rectangle created from two points:
下面类代表从两个点建立的矩形:
public class Rectangle
{
Point p1, p2;
public Point P1 { get { return p1; } set { p1 = value; } }
public Point P2 { get { return p2; } set { p2 = value; } }
}
An instance of Rectangle can be created and initialized as follows:
Rectangle的实例能够被如下初始化建立:
var r = new Rectangle {
P1 = new Point { X = 0, Y = 1 },
P2 = new Point { X = 2, Y = 3 }
};
which has the same effect as
这和下面的影响是等同的
var r = new Rectangle();
var __p1 = new Point();
__p1.X = 0;
__p1.Y = 1;
r.P1 = __p1;
var __p2 = new Point();
__p2.X = 2;
__p2.Y = 3;
r.P2 = __p2;
where __p1 and __p2 are temporary variables that are otherwise invisible and inaccessible.
__p1和__p2是临时变量,而这是可以不可见,并且不能够被准入的。
If Rectangle’s constructor allocates the two embedded Point instances
如果Rectangle的构造者分配了两个内嵌的Point实例
public class Rectangle
{
Point p1 = new Point();
Point p2 = new Point();
public Point P1 { get { return p1; } }
public Point P2 { get { return p2; } }
}
the following construct can be used to initialize the embedded Point instances instead of assigning new instances:
下面初始化能够被用来初始化内嵌的Point实例以替代分配新的实例:
var r = new Rectangle {
P1 = { X = 0, Y = 1 },
P2 = { X = 2, Y = 3 }
};
which has the same effect as
这和下面产生的影响是一致的
var r = new Rectangle();
r.P1.X = 0;
r.P1.Y = 1;
r.P2.X = 2;
r.P2.Y = 3;
A collection initializer specifies the elements of a collection.
集合构造者特指集合中的元素。
collection-initializer:
{ element-initializer-listopt }
{ element-initializer-list , }
element-initializer-list:
element-initializer
element-initializer-list , element-initializer
element-initializer:
non-assignment-expression
A collection initializer consists of a sequence of element initializers, enclosed by { and } tokens and separated by commas. Each element initializer specifies an element to be added to the collection object being initialized. To avoid ambiguity with member initializers, element initializers cannot be assignment expressions. The non-assignment-expression production is defined in §26.3.
集合构造者包含一组元素构造者,这些元素构造者由{}括起并且由逗号分隔。每个元素构造者特指某个能够在集合对象被初始化的过程中能够加入其中的元素。为了避免与成员构造者之间的混淆,元素构造者不能够被分配表达式。non-assignment-expression主题定义在§26.3.
The following is an example of an object creation expression that includes a collection initializer:
下面的例子是关于某个对象建立表达式,包含一个集合构造者:
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
The collection object to which a collection initializer is applied must be of a type that implements System.Collections.Generic.ICollection<T> for exactly one T. Furthermore, an implicit conversion (§6.1) must exist from the type of each element initializer to T. A compile-time error occurs if these requirements are not satisfied. A collection initializer invokes the ICollection<T>.Add(T) method for each specified element in order.
对于应用集合构造者的集合对象必须是某个实现了System.Collections.Generic.ICollection<T>中某个确实的T的类型。进一步说,隐式的转换必须存在于,对于T每个元素的类型。如果这些要求得不到满足,则会引发编译时刻的错误。集合构造者依顺序为每个特定的元素调用ICollection<T>.Add(T)函数。
The following class represents a contact with a name and a list of phone numbers:
下面的类表示了某个具有名字和一系列电话号码的联系人:
public class Contact
{
string name;
List<string> phoneNumbers = new List<string>();
public string Name { get { return name; } set { name = value; } }
public List<string> PhoneNumbers { get { return phoneNumbers; } }
}
A List<Contact> can be created and initialized as follows:
List<Contact>能够如下被建立和初始化:
var contacts = new List<Contact> {
new Contact {
Name = "Chris Smith",
PhoneNumbers = { "206-555-0101", "425-882-8080" }
},
new Contact {
Name = "Bob Harris",
PhoneNumbers = { "650-555-0199" }
}
};
which has the same effect as
这和下面的代码同样效果
var contacts = new List<Contact>();
var __c1 = new Contact();
__c1.Name = "Chris Smith";
__c1.PhoneNumbers.Add("206-555-0101");
__c1.PhoneNumbers.Add("425-882-8080");
contacts.Add(__c1);
var __c2 = new Contact();
__c2.Name = "Bob Harris";
__c2.PhoneNumbers.Add("650-555-0199");
contacts.Add(__c2);
where __c1 and __c2 are temporary variables that are otherwise invisible and inaccessible.
其中,__c1和__c2是临时变量,并且应该是不可见和不得准入。