Your first part of the code uses a collection initializer that does not use a logical assignment, but instead is intended to call Add into an existing collection. In other words, it is:
var x = new Dictionary<string, List<int>> { ["a"] = {1}, };
is equivalent to:
var tmp = new Dictionary<string, List<int>>(); var list = tmp["a"]; list.Add(1); var x = tmp;
Hopefully this is obvious from why the second extension line will throw an exception.
Part of your reasoning error:
although {1} is a pretty well-formed array
No no. The syntax {1} means different things in different contexts. In this case, it is the initializer of the collection. In a statement:
int[] a = { 1, 2, 3, 4 };
it is an array initializer. This syntax only creates a new array in an array declaration or as part of an array creation expression, for example. new[] { 1, 2, 3, 4 } .
source share