In-depth understanding of new features of C #3. x (5): Object Initializer and Collection Initializer

Source: Internet
Author: User

It has been two months since the beginning of C #3. x. In the previous chapter, we have discussed C #3 in depth. some new column features introduced by x: Anomynous Type, Extension Method, Lambda Expression, and Automatically Implemented Property. Today we will discuss the other two simple Feature involved in this series: object Initializer and Collection Initializer.

I,Why should we introduce Object Initializer?And Collection Initializer

When creating an object with many attributes, we often encounter this situation: in order to make our Code more concise, we try to call a suitable Constructor so that the object can be initialized for the required attributes during creation, however, we often cannot find such "perfect" Constructor to match the attribute list we need to initialize. Therefore, we usually call a relatively suitable Constructor to create the objects we need, and assign values to fields or properties that cannot be initialized in the Constructor. Now we have a good way to effectively solve this problem, that is, Object Initializer.

The above section describes how to create and initialize a general object. Now we often use the Collection interface System. Collections. IEnumerable to create and initialize the Collection. For such an object, we generally first create this object through Constructor, and then Add the required Element through the Add method or other methods. Now we can combine the two processes into one through Collection Initializer.

Next we will introduce how to use Object Initializer and Collection Initializer, and what is behind them: What Compiler did for us during compilation.

II,Object InitializerUsage and Nature

Object Initializer is easy to use: when creating an Object using the new keyword, copy the required Field/Proeprty to {} after the Type name. For example:

 

Class Program
{
Static void Main (string [] args)
{
Vector v = new Vector {X = 1, Y = 2 };
}
}

Class Vector
{
Public double X
{
Get;
Set;
}

Public double Y
{
Get;
Set;
}
}

Note: For Vector definition, another new feature of C #3. x is used: Automatically Implemented Proeprty.

In the above example, we use a code (Vector v = new Vector {X = 1, Y = 2 };) create a Vector object and initialize X & Y.

At the beginning of this series, I have been emphasizing C #3. x these Feature are only based on a new Feature in the Programming Language layer. These features are implemented through the Compiler corresponding to the Programming Language by adding some auxiliary Code during the compilation process. For the simple Code (Vector v = new Vector {X = 1, Y = 2};) in the preceding sentence, the compilation will look like this:

Vector <> g _ initLocal0 = new Vector ();
<> G _ initLocal0.X = 1;
<> G _ initLocal0.Y = 2;
Vector v = <> g _ initLocal0;

Through the analysis of the above code, we can conclude that Compiler implements Object Initializer through the following three steps.

  • Call the corresponding Constructor of the corresponding Class to create a temporary object.
  • Assign values to the Field/Property corresponding to the temporary object based on the value assignment statement in.
  • Assign this temporary object to the object you created.

III,Collection InitializerUse and Nature

Collection Initializer combines the creation of Collection objects with the initialization of elements. Its usage is similar to that of Object Initializer: Add a Class name directly to the Element List in:

IList <string> list = new List <string> {"Zhang San", "Li Si", "Wang Wu "};

The essence of Object Initializer is the same as that of Object Initializer. Then, let's take a look at what the Code after the Compiler variant looks like. We will have a comprehensive understanding of the implementation of Collection Initializer:

List <string> <> g _ initLocal0 = new List <string> ();

<> G _ initLocal0.Add ("Zhang San ");

<> G _ initLocal0.Add ("Li Si ");

<> G _ initLocal0.Add ("Wang Wu ");

IList <string> list = <> g _ initLocal0;


The implementation of Collection Initializer is similar to that of Object Initializer:
 

  • Call the Default Constructor (without parameters) of the corresponding Class to create a temporary object.
  • Based on the Value assignment statement in {}, Add the corresponding Element by calling the Add method.
  • Assign this temporary object to the object you created.

C # 3.x:
[Original] in-depth understanding of new features of C #3. x (1): Anonymous Type
[Original] in-depth understanding of new features of C #3. x (2): Extension Method-Part I
[Original] in-depth understanding of new features of C #3. x (2): Extension Method-Part II
[Original] in-depth understanding of new features of C #3. x (3): From Delegate, Anonymous Method to Lambda Expression
[Original] in-depth understanding of new features of C #3. x (4): Automatically Implemented Property
[Original] in-depth understanding of new features of C #3. x (5): Object Initializer and Collection Initializer

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.