Bug 33165 - Box Then Cast Defect
Summary: Box Then Cast Defect
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 1.4.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-08-17 13:42 UTC by jmeyer
Modified: 2015-08-24 11:06 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Sample code reproducing observed issue (32.92 KB, application/x-zip-compressed)
2015-08-17 13:42 UTC, jmeyer

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description jmeyer 2015-08-17 13:42:41 UTC
Created attachment 12535 [details]
Sample code reproducing observed issue

Attempting to cast a value that has been boxed results in the following error message & crashes, even when the cast should be valid: "Cannot cast from source type to destination type"

For example, the following code fragment would produce this error:

            int integer = 5;
            object boxedValue = integer;
            float castedToFloat = (float) boxedValue; //Cast error thrown

Please find attached example which demonstrates this behavior
Comment 1 Brian Bennewitz 2015-08-17 14:35:27 UTC
This is painful inside of IValueConverter implementations where the values come in pre-boxed.
Comment 2 Paul DiPietro [MSFT] 2015-08-24 11:06:32 UTC
Hi all,

This is not actually an issue with Forms and is expected behavior within C# and the compiler. If you attempt to do the same thing within a basic console application you will run into the InvalidCastException. For more information on this you can read this post by Eric Lippert: