Grund für die KeyNotFoundException in der Dictionary-Initialisierung
Der folgende Code
new Dictionary<string, List<int>> {
["a"] = {1},
};
Wirft eine LaufzeitKeyNotFoundException
, obwohl {1} ein perfekt geformtes Array ist (d. h.int[] a = {1,2,3,4}
als gültiger Code). WechselnTValue
desDictionary
zuint[]
, wirft eine KompilierungszeitCS1061
, dies jedoch nicht (beachten Sie das hinzugefügtenew[]
Array-Zuordnung):
new Dictionary<string, IEnumerable<int>> {
["a"] = new[]{1},
};
Warum passiert das