-1

I want to create a Dictionary of <string, Dictionary> type where the inner Dictionary is of type <string, List<string>>.

I wrote the following code for that :

using System.IO ; 
using System.Collections ; 
using System.Collections.Generic ; 

class nm {
    public static void Main () {
        Dictionary<string, Dictionary<string, List<string>>> dicdic = new Dictionary<string, Dictionary<string, List<string>>>();
        Dictionary<string, List<string>> fr = new Dictionary<string, List<string>>();
        Dictionary<string, List<string>> subdicdic = new Dictionary <string , List<string>>() ; 
        List<string> p = new List<string>(); 
        p.Add("bro, you feel me ") ;
        subdicdic.Add(new KeyValuePair<string, List<string>>("ll", p)); 
        fr.Add(new KeyValuePair("foo", subdicdic)) ;
    }
}

But on trying to compile it with csc in windows command prompt , I am getting a compilation error at line 18 which is the following :

 fr.Add(new KeyValuePair("foo", subdicdic)) ;

stating the following :

"No overload for method 'Add' takes '1' arguments "

I surfed up for similar questions on c# and I got the following question :

Why am i getting "No overload method for Add takes 1 argument" when adding a Dictionary to a List of Dictionaries

But after going through this I am getting a feel there might be some restriction for using list variable name in place of the list . But I am not sure .

I went through the following question too :

Adding a key value pair in a dictionary inside a dictionary

Although the above might help in getting an alternative solution for the purpose code that I am trying to write .

But for now I want to know what stops me from adding this list as a value for the dictionary .

The version details of my compiler :

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.8922 for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727

What might be the issue here ? How to operate with a dictionary having an internal dictionary with list in .net 2.0 ?

  • You say in your question you want two layers: `string, dictionary` where the dictionary is a `string, List`. But in your code you have 3 layers. Is this on purpose? – Jonathan Nov 21 '18 at 23:08
  • @jonathan :Did not get you.All I waned is to have a dictionary of type and the inner dictionary is of type > – kshiteez bizalom Nov 21 '18 at 23:23
  • Have you considered actually making a class to hold your dictionary value? Dictionaries of dictionaries get a bit ridiculous after a point, and we are pst that point here. – theMayer Nov 22 '18 at 00:17

2 Answers2

2

The short answer, you are trying to use an overload of a method that does not exist.

The Add method on a the Dictionary<T1, T2> class does not support passing in a KeyValuePair<T1, T2> directly. The only pattern available via the Add method is Add(T1 key, T2 value);.

You need to change this line: subdicdic.Add(new KeyValuePair<string, List<string>>("ll", p)); to subdicdic.Add("ll", p);

I should note that you will experience the exact same issue on the very next line and will need to make a similar adjustment.

For your reference, I have created a working .NET Fiddle example of this problem.

Shawn Lehner
  • 1,293
  • 7
  • 14
  • c#dictdict.cs(19,58): error CS1502: The best overloaded method match for 'System.Collections.Generic.Dictionary>.Add(string, System.Collections.Generic.List)' has some invalid arguments c#dictdict.cs(19,73): error CS1503: Argument '2': cannot convert from 'System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.List' – kshiteez bizalom Nov 21 '18 at 23:16
  • I updated my answer with a working .NET Fiddle of your problem. Your issue is because you are trying to add subdic to your fr collection. That is not correct. You need to add it to the dicdic collection. – Shawn Lehner Nov 22 '18 at 00:13
1

Did you try this way:

using System.IO;
using System.Collections;
using System.Collections.Generic;

class nm
{
    public static void Main()
    {
        var dicdic = new Dictionary<string, Dictionary<string, List<string>>>();
        var subdicdic = new Dictionary<string, List<string>>();
        var p = new List<string>();
        p.Add("bro, you feel me ");
        subdicdic.Add("ll", p);
        dicdic.Add("foo", subdicdic);
    }
}
kara
  • 130
  • 1
  • 11