by Paulo Morgado via Paulo Morgado : C# on 8/10/2008 11:27:22 PM
C# 3.0 introduced object and collection initializers. It is now easier to initialize objects or collections:
var person = new Person { FirstName = "Paulo", LastName = "Morgado" }; var persons = new List<Person> { new Person { FirstName = "Paulo", LastName = "Morgado" }, new Person { FirstName = "Luís", LastName = "Abreu" } }; var personDirectory = new Dictionary<string, Person> { { "Lisboa", new Person { FirstName = "Paulo", LastName = "Morgado" } }, { "Funchal", new Person { FirstName = "Luís", LastName = "Abreu" } } };
Wouldn't be nice to be able to do the same on already created objects and collections?
But, what would the syntax used be? Something like this?
var person = new Person(); person = { FirstName = "Paulo", LastName = "Morgado" }; var persons = new List<Person>(); persons += { new Person { FirstName = "Paulo", LastName = "Morgado" }, new Person { FirstName = "Luís", LastName = "Abreu" } }; var personDirectory = new Dictionary<string, Person>(); personDirectory += { { "Lisboa", new Person { FirstName = "Paulo", LastName = "Morgado" } }, { "Funchal", new Person { FirstName = "Luís", LastName = "Abreu" } } };
What do you think of this?
Original Post: How About Property Assignment And Collection Adding Like Object And Collection Initializers In C#?
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.