C#3.0規範(七)對象以及集合構造者

來源:互聯網
上載者:User
 

26.1 Object and collection initializers 對象以及集合構造者

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.

對於某個對象或集合的構造者而言,引用該對象執行個體進行初始化是不可能的。

26.1.1 Object initializers 物件建構者

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;

26.1.2 Collection initializers 集合構造者

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是臨時變數,並且應該是不可見和不得准入。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.