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"]);
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
When you add to the dictionary your double will be boxed, as the dictionary is mapping a string
to an object
.
When you unbox you must cast to the underlying type. In your case that underlying type is a double
, so the cast to float
will fail.
You can get around this by using Convert.ToSingle
netPlannedHours = Convert.ToSingle(d["key"])
This method will work out the underlying type and can do the conversion, at the expense of a performance hit when working out the type conversions.
I had a similar problem today with my SQL entity mapped as "float" and my .Net entity mapped as "float". I kept the SQL mapping, though changed the .Net types to "double".
The ADO.NET SQL Server Data Type Mappings documentation presents a table of the correct mapping for all types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With