9

I want to populate a List of string arrays at compile time

something like this:

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

but I get a compile error: No overloaded method 'Add' takes 2 arguments

Why is this? It works if they weren't string arrays with List<string>

4
  • You are treating your collection as a dictionary instead of a list. Commented Dec 3, 2015 at 22:40
  • Dictionaries can only contain a pair, I want to be able to contain more than a pair if necessary Commented Dec 3, 2015 at 22:41
  • In that case you might need some custom model class that takes your left value, eg. s0 or s5, and in that custom model add synonyms that are a list of strings. What you have at the moment is not going to work. Commented Dec 3, 2015 at 22:42
  • I don't want the left value to be treated differently than the other synonyms. Basically we have a huge codebase and I need to make "enabled" == "enable" Commented Dec 3, 2015 at 22:43

6 Answers 6

8

You aren't creating arrays in your example, try the following:

 List<string[]> synonyms = new List<string[]> { new[] { "enabled", "enable", "enabling" }, new[] { "disabled", "disable", "disabling" }, new[] { "s0", "s0 state" }, new[] { "s5", "s5 state" } }; 
Sign up to request clarification or add additional context in comments.

Comments

7

You need to add a string array

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

Comments

6

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 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, 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 you 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" }, }; 

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:

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

Comments

6

Create implicitly typed arrays for your List initializer:

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

new [] { ... } is equivalent to doing new string[] { ... } when the types of the objects in the array are all string. You will get an error using the implicit syntax if the types of the objects in the array are different.

Comments

5

You need to specify that the inner objects are arrays so that it doesn't look for multi-parameter Add methods.

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

Comments

1

I think it might be that you're not making any instances of the arrays.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.