title: C# Unable to cast object of type 'System.Double' to type 'System.Single'
body: <p>before judging that this question is already answered, please read the description. I have this simple code below:</p>
  <pre><code>Dictionary&lt;string, object&gt; d = new Dictionary&lt;string, object&gt;();
  
  d.Add(&quot;key&quot; , 30d);
  
  System.Diagnostics.Debug.WriteLine($&quot; TYPE OF OBJECT IS  \&quot;{d[&quot;key&quot;].GetType()}\&quot;&quot;);
  
  netPlannedHours = (float)d[&quot;key&quot;];         ---&gt; **Line of Interest**
  </code></pre>
  <p>When i execute this i get:</p>
  <blockquote>
  <p>TYPE OF OBJECT IS  &quot;System.Double&quot; Exception thrown:
  'System.InvalidCastException' in  DevOpsAutomatedReporting.dll Unable
  to cast object of type 'System.Double' to type 'System.Single'.</p>
  </blockquote>
  <p>The exception is caused by the last line tagged &quot;Line of interest&quot;. I can't really understand why the last line is causing this as the type of the object is inferred to be &quot;System.Double&quot; 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 (&quot;Line of interest&quot;) with either of the following two lines of code it successfully converts the double to float</p>
  <pre><code>// Cast the double object to double again and then to float **WORKS**
  netPlannedHours = (float)(double)d[&quot;key&quot;];
    
  // Convert to float using &quot;Convert.ToSingle()&quot;  **WORKS**
  netPlannedHours = Convert.ToSingle(d[&quot;key&quot;]);
  </code></pre>