Skip to main content
added 548 characters in body
Source Link
dbc
  • 120k
  • 27
  • 273
  • 404

In fact, it's possible to make such a collection and initialize it with this syntax. If Iyou create the following collection:

ThusYou can also make the following List subclass:

public class ListOfArray<T> : List<T[]> { public new void Add(params T[] args) { base.Add(args); } } 

And initialize it with the desired syntax:

 var synonyms = new ListOfArray<string> { { "enabled", "enable", "enabling" }, { "disabled", "disable", "disabling" }, { "s0", "s0 state" }, { "s5", "s5 state" } }; 

But for List<string []>, as others have noted, you need to initialize your synonyms list by explicitly allocating each string [] entry, rather than using an implicit collection intializer for each entry:

In fact, it's possible to make such a collection and initialize it with this syntax. If I create the following collection:

Thus, as others have noted, you need to initialize your synonyms list by explicitly allocating each string [] entry, rather than using an implicit collection intializer for each entry:

In fact, it's possible to make such a collection and initialize it with this syntax. If you create the following collection:

You can also make the following List subclass:

public class ListOfArray<T> : List<T[]> { public new void Add(params T[] args) { base.Add(args); } } 

And initialize it with the desired syntax:

 var synonyms = new ListOfArray<string> { { "enabled", "enable", "enabling" }, { "disabled", "disable", "disabling" }, { "s0", "s0 state" }, { "s5", "s5 state" } }; 

But for List<string []>, as others have noted, you need to initialize your synonyms list by explicitly allocating each string [] entry, rather than using an implicit collection intializer for each entry:

Source Link
dbc
  • 120k
  • 27
  • 273
  • 404

The reason that your code does not work is spelled out in How to: Initialize a Dictionary with a Collection Initializer (C# Programming Guide):

A Dictionary<TKey, TValue> contains a collection of key/value pairs. Its Add method takes two parameters, one for the key and one for the value. To initialize a Dictionary<TKey, TValue>, or any collection whose Add method takes multiple parameters, enclose each set of parameters in braces...

Thus, in the syntax

 { { "enabled", "enable", "enabling" }, } 

The compiler is expecting your collection to have an Add(string, string, string) method. Because there is no such method on List<string []>, your compilation fails.

In fact, it's possible to make such a collection and initialize it with this syntax. If I create the following collection:

public class TripleCollection<T> : IEnumerable<Tuple<T, T, T>> { readonly List<Tuple<T, T, T>> list = new List<Tuple<T, T, T>>(); public void Add(T first, T second, T third) { list.Add(Tuple.Create(first, second, third)); } public IEnumerator<Tuple<T, T, T>> GetEnumerator() { return list.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } 

The following will compile successfully and create a collection with one 3-tuple:

 var tuples = new TripleCollection<string> { { "1", "2", "3" }, }; 

Thus, as others have noted, you need to initialize your synonyms list by explicitly allocating each string [] entry, rather than using an implicit collection intializer for each entry:

 var synonyms = new List<string[]> { new [] { "enabled", "enable", "enabling" }, new [] { "disabled", "disable", "disabling" }, new [] { "s0", "s0 state" }, new [] { "s5", "s5 state" } };