C # 3.0 Language new features (language Specification): 5 anonymous Types

Source: Internet
Author: User
Tags anonymous expression new features
Specification

Original: "C # Version 3.0 Specification", Microsoft
Translation: Lover_p
C # 3.0 allows the new operator to be used with an anonymous object initializer to create an object of an anonymous type.

Primary-no-array-creation-expression:
...
Anonymous-object-creation-expression

Anonymous-object-creation-expression:
New Anonymous-object-initializer

Anonymous-object-initializer:
{member-declarator-listopt}
{member-declarator-list,}

Member-declarator-list:
Member-declarator
Member-declarator-list, Member-declarator

Member-declarator:
Simple-name
Member-access
identifier = expression

An anonymous object initializer declares an anonymous type and returns an instance of the type. An anonymous type is a class type that has no name and inherits directly from object. Members of an anonymous type are a series of read/write properties that are inferred in turn by the object initializer used when creating an instance of that type. Specifically, an anonymous object initializer has the following form:

New {P1 = e1, p2 = e2, ... pn = en}

Declares an anonymous type with the following form:

Class __anonymous1

{

Private T1 F1;

Private T2 F2;

...

Private TN fn;

Public T1 P1 {get {return f1;} set {f1 = value;}}

Public T2 P2 {get {return f2;} set {F2 = value;}}

...

Public Tn pn ({get) {return fn;} set {fn = value;}}

}

Each of these TX is the type of the corresponding expression ex. If an expression in the anonymous object initializer has a null type, a compile-time error occurs.

The name of the anonymous type is automatically generated by the compiler and cannot be referenced in the program text.

In the same program, two anonymous object initializers with the same name, same type, and same order attribute will produce an instance of the same anonymous type. (This definition includes the order of the attributes, because in some environments the order is visible and important, such as reflection.) )

The following example:

var P1 = new {Name = ' lawnmower ', price = 495.00};

var P2 = new {Name = ' shovel ', Price = 26.95};

P1 = p2;

The assignment in the last row is allowed because P1 and P2 have the same anonymous type.

A member initializer can be abbreviated to a simple name or a member access. The member initializer is said to be a divergent initializer (projection initializer) and a shorthand for declarations and assignments of attributes with the same name. In particular, a member declaration has the following form:

Identifier
Expr. Identifier

Exactly equivalent to the following form:

identifier = Identifier
identifier = expr. Identifier

Therefore, in a divergent initializer, identifier also selects the values and fields or properties of the assigned value. Intuitively, a divergent initializer reflects more than just a value, including the name of the value.



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.