7

I get the error Using the generic type 'System.Collections.Generic.Dictionary< TKey, TValue>' requires 2 type arguments with this line of code:

this._logfileDict = new Dictionary();

I am just trying to have a clear _logfileDict with no entries in it in my code, so that is why I am assigning it a new Dictionary, which will be empty, but if anybody knows some code I could use to just empty _logfileDict otherwise. Its just a dictionary declared as follows:

private Dictionary<string, LogFile> _logfileDict;

Any help is much appreciated!

2
  • 1
    IntelliSense should point you in the right direction once you type '= new'...
    – jv42
    Nov 16, 2011 at 14:43
  • 2
    The answer is in the error message.
    – jrummell
    Nov 16, 2011 at 14:43

5 Answers 5

16

The reason you are getting this error is because you are attempting to use the Dictionary<TKey, TValue> class which requires generic type arguments and there is no Dictionary class which does not require generic type arguments.

When using a class which uses generic type arguments you are required to supply the types you want to use whenever you declare or instantiate a variable dealing with the class.

Replace:

this._logfileDict = new Dictionary();

With:

this._logfileDict = new Dictionary<string, LogFile>();
3
  • That's a bit short for an answer, but correct nonetheless :).
    – jv42
    Nov 16, 2011 at 14:42
  • 1
    @jv42 Agreed, I've updated the answer to include some more information and references. Nov 16, 2011 at 14:57
  • 1
    Oooh, nice this is now a truly great answer :)
    – jv42
    Nov 16, 2011 at 15:32
6

There is no type Dictionary without generic parameters in the .NET Framework. You must explicitly state the type parameters when instantiating and instance of Dictionary<TKey, TValue>. In your case, _logfileDict is declared as Dictionary<string, LogFile> and therefore you must explicitly state this when you assign a new instance of it. Therefore, you must assign to _logfileDict a new instance thusly:

this._logfileDict = new Dictionary<string, LogFile>();

(Note, however, there is a System.Collections.Hashtable in the .NET Framework if you don't want to specify the type of the keys and values.)

2

You need to specify the two types:

this._logfileDict = new Dictionary<string, LogFile>();
2

try this ..

 this._logfileDict = new Dictionary<string, LogFile>(); 

its worth to read about dictionaries and how to intialise, pls go through this link for more information on dictionary...

1

Well as you are aware the constructor in your Dictionary requires type for your key and type for your value so that is the error you are getting.

And because your logfile dictionary def has type of string and type of Logfile than surely the constructor is

this._logfileDict = new Dictionary<string, LogFile>(); 

This is an empty (clear as you defined) dicitonary but it needs this definition to be initalised so that when you do enter stuff to it, it can type check

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.