Using the generic type 'System.Collections.Generic.Dictionary <TKey, TValue>' requires 2 type arguments

I get an 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'm just trying to get a clear _logfileDict without entries in it in my code, so I assign it a new dictionary that will be empty, but if anyone knows some code that I could use to just delete _logfileDict otherwise. His simple dictionary is declared as follows:

 private Dictionary<string, LogFile> _logfileDict; 

Any help is much appreciated!

+4
source share
5 answers

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

When using a class that uses generic type arguments, you need to specify the types that you want to use when you declare or instantiate a variable related to the class.

Replace:

 this._logfileDict = new Dictionary(); 

WITH

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

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

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

(Note that the .NET Framework has System.Collections.Hashtable if you do not want to specify the type of keys and values.)

+6
source

You need to specify two types:

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

try it.

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

it is worth reading about dictionaries and how to intialise, pls follow this link for more information about the dictionary ...

+2
source

Well, as you know, the constructor in your dictionary requires a type for your key and enter for your value so that this is the error you get.

And since your log def dictionary file has a line type and a log file type, then, of course, the constructor

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

This is an empty (clear as you defined) dicitonary, but this requires that this definition be initialized so that when you enter material into it, it can enter a check

Hope this helps

+1
source

Source: https://habr.com/ru/post/1381499/


All Articles