C # 3.0 Language new features (language Specification): 4 Objects and collection initializers

Source: Internet
Author: User
Objects | specifications | collection

Original: "C # Version 3.0 Specification", Microsoft
Translation: Lover_p
An object-creation expression can contain an object or collection initializer that initializes a member of a newly created object or an element of a newly created collection.

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 a constructor argument list and replace it with parentheses as an object or collection initializer. Omitting a constructor argument list and replacing it with parentheses as an object or collection initializer is equivalent to specifying an empty argument list.

When an object creation expression with an object or collection initializer is executed, the instance constructor is invoked first, and then the member or element initialization specified by the object or collection initializer is executed.

An object or collection initializer cannot reference an object instance that is being initialized.

4.1 Object Initializers

The object initializer specifies the value of one or more fields or properties of an object.

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

The object initializer consists of a series of member initializers, surrounded by the {and} tokens, separated by commas. Each member initializer begins with the name of an accessible field or property of an object followed by an equal sign, followed by an expression or an object or collection initializer. An error occurs if the object initialization includes more than one member initializer for the same domain or property.

The handling of the member initializer for the expression specified after the equals sign is consistent with the assignment of the domain and property.

The member initializer of the object initializer, after the equal sign, is also initialized to a nested object. Unlike assigning a new value to a field or property, an assignment in an object initializer is considered to be assigned to a member of a field or property. A property with a value type cannot be initialized through this construct.

The member initializer of the collection initializer specified after the equal sign is also initialized to a nested collection. Unlike assigning a new collection to a field or property, the element given in the initializer is added to the collection referenced by the field or property. The field or property must be a collection type that meets the requirements specified in the next section.

The following class table is a point with two coordinate values:

public class Point

{

int x, y;

public int × {get {return X;} set {x = value;}}

public int y {get {return y;} set {y = value;}}

}

An instance of point can be created and initialized as follows:

var a = new Point {X = 0, Y = 1};

It is equivalent to:

var a = new Point ();

a.x = 0;

A.Y = 1;

The following class table is a rectangle with two dots:

public class Rectangle

{

Point P1, p2;

Public point P1 {get {p1;} set {p1 = value;}}

Public point P2 {get {p2;} set {P2 = value;}}

}

You can create and initialize a rectangle as follows:

var r = new Rectangle {

P1 = new Point {X = 0, Y = 1},

P2 = new Point {X = 2, Y = 3}

};

It is equivalent to:

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;

The __P1 and __P2 are temporary variables that are not visible and inaccessible elsewhere.

If the rectangle constructor assigns two nested point instances:

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 constructs can be used to initialize an embedded point instance instead of assigning it a new value:

var r = new Rectangle {

P1 = {X = 0, Y = 1},

P2 = {X = 2, Y = 3}

};

It is equivalent to:

var r = new Rectangle ();

R.p1. X = 0;

R.p1. Y = 1;

R.p2. X = 2;

R.p2. Y = 3;

4.2 Assembly initializers

The collection initializer specifies the elements of the 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 series of element initializers, surrounded by {and} tokens, and separated by commas. Each element initializer specifies an element that is added to the collection object to be initialized. To avoid confusion with the member initializer, the element initializer cannot be an assignment expression.

The following is an example of an object-creation expression that contains a collection initializer:

list<int> digits = new List<int> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

The type of the object that can apply the collection initializer must implement the system.collections.generic.icollections<t> and specify the T. In addition, there must be an implicit conversion from the type of each element initializer to T. If these conditions are not met, a compile-time error is generated. The collection initializer will call ICOLLECTION<T> for each specified element in turn;. ADD (T).

The following class table is an address book with a name and a set of phone numbers:

public class contacts

{

String name;

list<string> phonenumbers = new list<string> ();

public string Name {' Get {# ' #

Public list<string> Phonenumbers {get {return phonenumbers}}

}

You can create and initialize a list<contact> as follows:

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"}

}

};

It is equivalent to:

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 not visible and inaccessible elsewhere.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.