1

I am trying to code in C#, but I find that I can't construct an instance in this way like C++:

Dictionary<string, List<string>> FirSet =
    new Dictionary<string, List<string>>() {
        { "a", {"ab", "abc"} },
        { "b", {"bc", "bcd"} }
    };
Philippe
  • 3,945
  • 3
  • 38
  • 56
Bowen Peng
  • 1,635
  • 4
  • 21
  • 39
  • 6
    You need `new List()` before your list items. So `{ "a", {"ab", "abc"} }` should become `{ "a", new List() {"ab", "abc"} }` – maccettura Apr 16 '18 at 14:54
  • 1
    Do you really need a `List` or can it be a `string[]` array? You can use that initialization syntax with arrays. For lists, you need to new one up for each dictionary value. – itsme86 Apr 16 '18 at 14:54
  • 1
    @itsme86 wouldnt you still need to new up an array in that case? `new [] { "some", "example" }`? – maccettura Apr 16 '18 at 14:55
  • 1
    You're also missing a closing `}` – DavidG Apr 16 '18 at 14:55
  • @maccettura Oops, you're right. – itsme86 Apr 16 '18 at 14:59
  • I'd like to throw out another viewpoint: your structure might be getting convoluted enough that you might not want to one-line it. If I was creating multiple entries in a nested structure like this, I'd break it out into a blank new Dictionary<...> line, followed by a myDict.Add(...) line for each element to add to the dictionary. It's around the same level of readability (maybe a bit more readable) and it's a shade more maintainable (since you don't have to worry so much about all the various brackets.) – Kevin Apr 16 '18 at 15:02

2 Answers2

6

You have to initiate the lists within the dictionary with new List<string>()

Here how it should work:

Dictionary<string, List<string>> FirSet = new Dictionary<string, List<string>>()
{
    { "a", new List<string> { "ab", "abc" }},
    { "b", new List<string> { "bc", "bcd" }}
};
dmigo
  • 2,849
  • 4
  • 41
  • 62
Lucion
  • 102
  • 5
  • 1
    Minor point, but if you're going to drop the `()` (which I prefer) from the latter list creation, you should also do it for the first one. – DavidG Apr 16 '18 at 14:58
  • 2
    Since our answers are (now) the same, I've deleted mine and upvoted yours. – Zohar Peled Apr 16 '18 at 15:01
  • @DavidG yes you are completaly right, its unnecessary, but since its possible i wanted to show both ways. might help FreAk Apoint in the future – Lucion Apr 16 '18 at 15:11
  • @ZoharPeled thank you, to be fair i just threw the answer out, dmigo was so kind to format it like it is – Lucion Apr 16 '18 at 15:12
2
    var firSet =
        new Dictionary<string, List<string>>()
        {
            { "a", new List<string>() {"ab", "abc"} },
            { "b", new List<string>() {"bc", "bcd"} }
        };

You can also use the C# 6.0 syntax:

    var firSet =
        new Dictionary<string, List<string>>()
        {
            ["a"] = new List<string> {"ab", "abc"},
            ["b"] = new List<string> {"bc", "bcd"}
        };

And if you feel particularly lazy and you don't want to type all these new List<string> you can always do this:

    var firSet =
        new Dictionary<string, string[]>()
        {
            { "a", new[] {"ab", "abc"} },
            { "b", new[] {"bc", "bcd"} }
        }
        .ToDictionary(i => i.Key, i => i.Value.ToList());
Philippe
  • 3,945
  • 3
  • 38
  • 56