'Declare an implicit variable in C# Directory
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
Solution 1:[1]
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"];
Solution 2:[2]
You can use object:
var serializeMetadata = new Dictionary<string, object>();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aditya Jain |
| Solution 2 |
