Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3226552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:28:13+00:00 2026-05-17T16:28:13+00:00

Ok I must be overlooking something extremely simple but I am lost. Given this

  • 0

Ok I must be overlooking something extremely simple but I am lost.

Given this

object val = -1;
var foo = (Int32)(val);
var bar = (Int64)(val);

The cast to Int64 throws and InvalidCastException.

I recognize this is related to some strangeness with boxing but I don’t understand the reasoning.

From what I understand val is boxed as Int32 on the first line.

Then when I try to cast as something other than Int32 InvalidCastException is thrown. I suppose this means that I am trying to unbox val as Int64 when it is actually an Int32?

Still seems strange. Couldn’t the cast unbox the value and then try to perform the cast?

Something like (Obviously this is horribly oversimplified, maybe the boxed type isn’t known so this isn’t possible?):

object val = -1;
Int32 unboxed = (Int32)(val);
var bar = (Int64)(unboxed);

Someone (read: Eric Lippert) School me on the reasoning behind this.

UPDATE: From Eric’s Blog that Reed posted a link to this is the succinct answer I was looking for

“…This would be a huge amount of code to generate, and it would be very slow. The code is of course so large that you would want to put it in its own method and just generate a call to it. Rather than do that by default, and always generate code that is slow, large and fragile, instead we’ve decided that unboxing can only unbox to the exact type. If you want to call the slow method that does all that goo, it’s available – you can always call Convert.ToInt32, which does all that analysis at runtime for you. We give you the choice between “fast and precise” or “slow and lax”, and the sensible default is the former. If you want the latter then call the method….”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:28:13+00:00Added an answer on May 17, 2026 at 4:28 pm

    This is because you can’t unbox and perform a conversion in a single operation. You must unbox the Int32 value into an Int32, and then subsequently convert its type.

    Because of that, this requires the object to be unboxed, then converted to Int64:

    object val = -1;
    int foo = (Int32)val;
    Int64 bar = (Int64)(Int32)val;
    

    Eric Lippert covered this in detail on his blog post titled Representation and Identity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.