before judging that this question is already answered, please read the description. I have this simple code below:
Dictionary<string, object> d = new Dictionary<string, object>();
d.Add("key" , 30d);
System.Diagnostics.Debug.WriteLine($" TYPE OF OBJECT IS \"{d["key"].GetType()}\"");
netPlannedHours = (float)d["key"]; ---> **Line of Interest**
When i execute this i get:
TYPE OF OBJECT IS "System.Double" Exception thrown: 'System.InvalidCastException' in DevOpsAutomatedReporting.dll Unable to cast object of type 'System.Double' to type 'System.Single'.
The exception is caused by the last line tagged "Line of interest". I can't really understand why the last line is causing this as the type of the object is inferred to be "System.Double" at runtime so it should've had cast it to a float but it doesn't. An interesting point is that if i replace the last line ("Line of interest") with either of the following two lines of code it successfully converts the double to float
// Cast the double object to double again and then to float **WORKS**
netPlannedHours = (float)(double)d["key"];
// Convert to float using "Convert.ToSingle()" **WORKS**
netPlannedHours = Convert.ToSingle(d["key"]);