C #3.0 new features: Object and collection Initializers

Source: Internet
Author: User

Public class Person
{
Public string FirstName {set; get ;}
Public string LastName {set; get ;}
Public int Age;
}
The previous process of assigning values to members of the Person class is as follows:
Person person = new Person ();
Person. FirstName = "Jack ";
Person. LastName = "li ";
Person. Age = 11;
Using the new features of object initializer, you can write the above implementation process
Person person = new Person () {FirstName = "Jack", LastName = "li", Age = 11 };
Can be further written
Person person = new Person {FirstName = "Jack", LastName = "li", Age = 11 };
The execution process of the first IL generated by this code is the same. This can simplify our code and allow a few lines of code to be written.

To better understand object initializer, pay attention to the following points:
1. Can the object initiator assign values to only some members, regardless of their order;
2. Only attribute data members of the public and internal types can be assigned values for the end-of-day initialization;
3: Can the object initializer be used together with the constructor? The constructor executes the constructor first.

Collection Initializers
When you understand the object initiator, you can better understand the set initiator, as shown in figure
Var strList = new List <string> {"a", "B "}
Var persons = new List <Person>
{
New Person {FirstName = "yang "},
New Person {LastName = "peng "}
}

In the set initialization constructor, You can construct a null value for the set.

Var persons = new List <Person>
{
New Person {FirstName = "yang "},
Null;
New Person {LastName = "peng "}
}
Everything is not perfect, and the initialization constructor also has advantages and disadvantages. The biggest advantage is that it can be combined with linq to make it simpler to write. In my opinion, the bad thing is that set property exposes the interface to the outside. If it is only initialized once, the constructor can be competent for this job and has better encapsulation. Note that object initialization does not necessarily require data members to implement set property. In some cases, get is also possible. Let's look at the example below:

Public class Contact
{
Private List <string> names = new List <string> {"first "};

Public List <string> Names
{
Get {return phoneNumbers ;}
}
}

Var contact = new Contact {Names = {"second "}};
This Names is the get operation, which is equivalent to the contact. names. add ("second"); in this example, a problem is also exposed: when the internal data member IEnumerable, do not directly return the member through the get method, which is not safe. This is because, after obtaining the Member from the outside, the class member is modified directly. This destroys Data encapsulation.
Any new things are a double-edged sword. The key lies in the people who use them. You can use the new features as needed.

Related Article

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.