Throwing an Invalid Cast Exception?... Look Out for Unboxing

Implicit and explicit numeric casts are usually pretty straightforward:
  1. int i = 5;  
  2. float f = i; // Implicit cast from Int32 to Single  
  3. Byte b = (Byte) i; // Explicit cast from Int32  to Byte  
If you’re not paying attention, a safe explicit numeric cast can throw a runtime exception:
  1. Object i = 5;  
  2. float l = (float) i; // System.InvalidCastException: Specified cast is not valid.   
  3. ArrayList a = new ArrayList();  
  4. int b;              
  5. for (int i = 0; i < 10; i++) {  
  6.     b = 10;    
  7.     a.Add(b);          
  8. }  
  9. float f = (float) a[0]; // System.InvalidCastException: Specified cast is not valid.  
Why is a cast from int to float ok, but a cast from int ArrayList[i] to float not ok?

Type Casting vs Numeric Casting

Our InvalidCastException is not coming from our Explicit Cast from int to float. Our exception is coming from our Unboxing an int to float.
A key difference between Value Types and Reference Types is where they get allocated. Value Types are allocated in the current Thread Stack. Reference Types are allocated in the Managed Heap (think garbage collection).
Boxing is the act of casting a Value Type to a Reference Type.
We are Boxing when we do things like:
  • Assign value types to reference type fields.
  • Add value types to a collection reference type.
  • Pass a value type as a parameter to a method that takes a reference type.
Unboxing is the act of casting a Reference Type to a Value Type.

We are Unboxing when we do things like:
  • Extract a value type from a reference type.
  • Extract a value type from a collection of value types.
  • Pass a boxed value type to a method that takes a value type.

Unboxing’s Constraints

During Unboxing, two constraints get checked by the CLR:
  1. If the variable containing the reference to the boxed value type instance is null, a NullReferenceException is thrown.
  2. If the reference doesn’t refer to an object that is a boxed instance of the desired value type, an InvalidCastException is thrown.
Unboxing to a Value Type that’s different from the originally Boxed Value Type is not allowed.
  1. Object i = 5; // Boxing int 5 into reference type Object  
  2. float l = (float)i; // Trying to unbox int b to float  
  3. ArrayList a = new ArrayList();  
  4. int b;              
  5. for (int i = 0; i < 10; i++)
  6. {  
  7.     b = 10;    
  8.     a.Add(b);  // Boxing int b into reference type ArrayList        
  9. }  
  10. float f = (float) a[0]; // Trying to unbox int b to float   
So now that we understand the problem, what’s the solution?

System.Convert Namespace

If you look at the .NET Reference Source for System.Int32, you’ll notice that it implements IConvertible.
  1. public struct Int32 :   
  2.     IComparable,  
  3.     IFormattable,  
  4.     IConvertible,   
  5.     IComparable<Int32>,  
  6.     IEquatable<Int32>  
Value Types like System.Int32 that implement IConvertible use the static System.Convert to do our Unboxing and Explicit Casting.
Here’s the definition of System.Convert.ToSingle from the .NET Reference Source:
  1. /// <internalonly/>  
  2. float IConvertible.ToSingle(IFormatProvider provider) 
  3. {  
  4.     return Convert.ToSingle(m_value);  
  5. }  
  6. Our corrected examples:  
  8. Object o = 5;  
  9. float f = Convert.ToSingle(o);  
  11. ArrayList a = new ArrayList();  
  12. int b;              
  13. for (int i = 0; i < 10; i++) 
  14. {  
  15.     b = 10;    
  16.     a.Add(b);          
  17. }  
  18. Convert.ToSingle(a[0]);  


We ran into a runtime exception when trying to do an explicit numeric cast that is usually successful.
The real cause of the issue is the way that we were casting from a Value Type to a Reference Type and then back to a Value Type. This is called Boxing and Unboxing. One of the rules of Unboxing is that we can only Unbox into the same Value Type we started with. Otherwise, we throw an InvalidCastException at runtime.
Our solution is to take advantage of the System.Convert static class. It provides us with a .ToXXX method that can be called for Value Types that implement IConvertible.
Hope this finds its way to you if/when you run into this problem, or it helps you stay aware of what the CLR is doing behind the scenes. As always, feel free to leave a comment below.

Recommended Free Ebook
Similar Articles