C # new features of the language: Collection initializers)

Source: Internet
Author: User
C # new features of the language: Collection initializers)

The object initializer is great, which greatly simplifies the process of adding an object to a set. For example, if I want to add three people to a list set based on the generic type person, I can write the following code:

List <person> People = new list <person> ();

People. Add (new person {firstname = "Scott", lastname = "Guthrie", age = 32 });
People. Add (new person {firstname = "bill", lastname = "Gates", age = 50 });
People. Add (new person {firstname = "Susanne", lastname = "Guthrie", age = 32 });

For this example, followCodeIn contrast, using the new object initializer feature alone saves 12 additional lines of code.

However, the C # and VB compilers in orcas allow us to go further and now support "collection initializers", which allows us to avoid writing multiple add statements, save More keyboard operations:

List <person> People = new list <person> {
New person {firstname = "Scott", lastname = "Guthrie", age = 32 },
New person {firstname = "bill", lastname = "Gates", age = 50 },
New person {firstname = "Susanne", lastname = "Guthrie", age = 32}
};

When the compiler encounters such a syntax, it will automatically generate a set inserted encoding as in the previous example.

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.