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.