collection-initializer

Empty collection initializer for list property results in null

*爱你&永不变心* 提交于 2020-01-11 11:02:09
问题 When I run this code, it doesn't initialize ThisIsAList to an empty collection as I was expecting... instead ThisIsAList was null. void Main() { var thing = new Thing { ThisIsAList = {} }; Console.WriteLine(thing.ThisIsAList == null); // prints "True" } public class Thing { public List<string> ThisIsAList { get; set; } } Why isn't this a compile error? Why is the result null ? I was wondering if maybe there was an implicit conversion going on here, but the following attempts produced compile

How to call a method with the C# collection initializer?

旧巷老猫 提交于 2019-12-13 02:10:45
问题 Case This morning I refactored some Logging method and needed to change a method's 'params' parameter in a normal array. Consequently, the call to the method had to change with an array parameter. I'd like the method call to change as less as possible, since it's a heavily used utility method. I assumed I should be able to use the collection initializer to call the method, but it gave me a compile-error. See the second call in the example below. The third call would be fine too, but also

Anonymous collection initializer for a dictionary

久未见 提交于 2019-12-10 02:38:45
问题 Is it possible to implicitly declare next Dictionary<HyperLink, Anonymous> : { urlA, new { Text = "TextA", Url = "UrlA" } }, { urlB, new { Text = "TextB", Url = "UrlB" } } so I could use it this way: foreach (var k in dic) { k.Key.Text = k.Value.Text; k.Key.NavigateUrl = k.Value.Url; } ? 回答1: How about: var dict = new[] { new { Text = "TextA", Url = "UrlA" }, new { Text = "TextB", Url = "UrlB" } }.ToDictionary(x => x.Url); // or to add separately: dict.Add("UrlC", new { Text = "TextC", Url =

Initialize the Attributes property of a WebControl object using collection initializer

女生的网名这么多〃 提交于 2019-12-06 04:52:01
问题 I want to initialize WebControl objects, inline, but for some fields this is a little bit tricky. For instance when I try to initialize the Attributes property of a TextBox object like this: using System.Web.UI.WebControls; Panel panel = new Panel() { Controls = { new TextBox() { Attributes = { { "key", "value" } } } } }; I get the error: Cannot initialize type 'AttributeCollection' with a collection initializer because it does not implement 'System.Collections.IEnumerable' Any idea how could

Why are collection initializers on re-assignments not allowed?

混江龙づ霸主 提交于 2019-12-04 17:45:48
问题 I always thought it worked fine both ways. Then did this test and realized it's not allowed on re-assignments: int[] a = {0, 2, 4, 6, 8}; works fine but not: int [ ] a; a = { 0, 2, 4, 6, 8 }; Any technical reason for this? I thought I would ask about it here, because this behavior was what I expected intuitively. 回答1: First off, let's get the terms correct. That's not a collection initializer. That's an array initializer . A collection initializer always follows a constructor for a collection

Empty collection initializer for list property results in null

我是研究僧i 提交于 2019-12-02 03:31:41
When I run this code , it doesn't initialize ThisIsAList to an empty collection as I was expecting... instead ThisIsAList was null. void Main() { var thing = new Thing { ThisIsAList = {} }; Console.WriteLine(thing.ThisIsAList == null); // prints "True" } public class Thing { public List<string> ThisIsAList { get; set; } } Why isn't this a compile error? Why is the result null ? I was wondering if maybe there was an implicit conversion going on here, but the following attempts produced compile errors: thing.ThisIsAList = Enumerable.Empty<string>().ToArray(); List<int> integers = { 0, 1, 2, 3 };

C#6's new Collection Initializer - Clarification?

邮差的信 提交于 2019-11-30 11:19:14
I've read that : The team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object But looking at : var Dic = new Dictionary<string,int>{ {"x",3}, {"y",7} }; VS var Dic = new Dictionary<string,int>{ ["x"]=3, ["y"]=7 }; I don't see where the benefit is. it looks the same. Both are nothing more than a name-value collection. They swapped pairs of curly braces for pairs of square brackets and some commas Question: What is the added value for using the new syntax ? a real world example would be much appreciated. The main

C#6's new Collection Initializer - Clarification?

假装没事ソ 提交于 2019-11-29 16:58:16
问题 I've read that : The team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object But looking at : var Dic = new Dictionary<string,int>{ {"x",3}, {"y",7} }; VS var Dic = new Dictionary<string,int>{ ["x"]=3, ["y"]=7 }; I don't see where the benefit is. it looks the same. Both are nothing more than a name-value collection. They swapped pairs of curly braces for pairs of square brackets and some commas Question: What is the

Combining List initializer and object initializer

☆樱花仙子☆ 提交于 2019-11-29 09:17:39
Is is possible to combine a List initializer and object initializer at the same time? Given the following class definition: class MyList : List<int> { public string Text { get; set; } } // we can do this var obj1 = new MyList() { Text="Hello" }; // we can also do that var obj2 = new MyList() { 1, 2, 3 }; // but this one doesn't compile //var obj3 = new MyList() { Text="Hello", 1, 2, 3 }; Is this by design or is it just a bug or missing feature of the c# compiler? No, looking at the definitions from section 7.6.10 of the C# spec, an object-or-collection-initializer expression is either an

Combining List initializer and object initializer

邮差的信 提交于 2019-11-28 02:40:19
问题 Is is possible to combine a List initializer and object initializer at the same time? Given the following class definition: class MyList : List<int> { public string Text { get; set; } } // we can do this var obj1 = new MyList() { Text="Hello" }; // we can also do that var obj2 = new MyList() { 1, 2, 3 }; // but this one doesn't compile //var obj3 = new MyList() { Text="Hello", 1, 2, 3 }; Is this by design or is it just a bug or missing feature of the c# compiler? 回答1: No, looking at the