object-initializer

How can I use collection initializer syntax with ExpandoObject?

徘徊边缘 提交于 2019-12-20 18:49:07
问题 I've noticed that the new ExpandoObject implements IDictionary<string,object> which has the requisite IEnumerable<KeyValuePair<string, object>> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to add properties to the expando object in the same way as you add items to a dictionary. Dictionary<string,object> dict = new Dictionary<string,object>() { { "Hello", "World" } }; dynamic obj = new ExpandoObject() { { "foo", "hello" }, { "bar", 42 },

Should I use an object initializer or a constructor? [duplicate]

橙三吉。 提交于 2019-12-07 04:01:15
问题 This question already has answers here : What's the difference between an object initializer and a constructor? (7 answers) Closed 6 years ago . I have just learned about object initializers and was wondering what the best practices for when to use them are. This is what I read about them: http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx It makes it clear that they are necessary for creating anonymous types but I would like to know if I should try to prefer them to normal

Should I use an object initializer or a constructor? [duplicate]

假装没事ソ 提交于 2019-12-05 08:03:12
This question already has an answer here: What's the difference between an object initializer and a constructor? 7 answers I have just learned about object initializers and was wondering what the best practices for when to use them are. This is what I read about them: http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx It makes it clear that they are necessary for creating anonymous types but I would like to know if I should try to prefer them to normal constructors in all the other cases. I would like to know if I should try to prefer them to normal constructors in all the other

How can I use collection initializer syntax with ExpandoObject?

徘徊边缘 提交于 2019-12-03 05:17:35
I've noticed that the new ExpandoObject implements IDictionary<string,object> which has the requisite IEnumerable<KeyValuePair<string, object>> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to add properties to the expando object in the same way as you add items to a dictionary. Dictionary<string,object> dict = new Dictionary<string,object>() { { "Hello", "World" } }; dynamic obj = new ExpandoObject() { { "foo", "hello" }, { "bar", 42 }, { "baz", new object() } }; int value = obj.bar; But there doesn't seem to be a way of doing that.

What&#39;s the difference between an object initializer and a constructor?

 ̄綄美尐妖づ 提交于 2019-11-26 00:17:40
问题 What are the differences between the two and when would you use an \"object initializer\" over a \"constructor\" and vice-versa? I\'m working with C#, if that matters. Also, is the object initializer method specific to C# or .NET? 回答1: Object Initializers were something added to C# 3, in order to simplify construction of objects when you're using an object. Constructors run, given 0 or more parameters, and are used to create and initialize an object before the calling method gets the handle