Home > Back-end >  Declare an implicit variable in C# Directory
Declare an implicit variable in C# Directory

Time:03-17

I have met a issues that I couldn't declare var into Dictionary in c#. Is there any way to declare a implicity datatype in Dictionary in c#?

The current declaration of my codes

IDictionary<string, var> serializeMetadata = new Dictionary<string, var>();

With the shown code, my Visual Studio IDE returns an error of

The contextual keyword "var" may only appear within a local variable declaration or in script code

CodePudding user response:

You can use object. Below is the basic example:-

Dictionary<string, object> storage = new Dictionary<string,object>();

storage.Add("age", 12);
storage.Add("name", "test");
storage.Add("bmi", 24.1);

int a = (int)storage["age"];
string b = (string)storage["name"];
double c = (double)storage["bmi"];

CodePudding user response:

You can use object:

var serializeMetadata = new Dictionary<string, object>();
  • Related