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

  • SEARCH
  • Home
  • 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 778069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:42:26+00:00 2026-05-14T19:42:26+00:00

When doing an upcast or downcast, what does really happen behind the scenes? I

  • 0

When doing an upcast or downcast, what does really happen behind the scenes? I had the idea that when doing something as:

string myString = "abc";
object myObject = myString;
string myStringBack = (string)myObject;

the cast in the last line would have as only purpose tell the compiler we are safe we are not doing anything wrong. So, I had the idea that actually no casting code would be embedded in the code itself. It seems I was wrong:

.maxstack 1
.locals init (
    [0] string myString,
    [1] object myObject,
    [2] string myStringBack)
L_0000: nop 
L_0001: ldstr "abc"
L_0006: stloc.0 
L_0007: ldloc.0 
L_0008: stloc.1 
L_0009: ldloc.1 
L_000a: castclass string
L_000f: stloc.2 
L_0010: ret 

Why does the CLR need something like castclass string?

There are two possible implementations for a downcast:

  1. You require a castclass something. When you get to the line of code that does an castclass, the CLR tries to make the cast. But then, what would happen had I ommited the castclass string line and tried to run the code?
  2. You don’t require a castclass. As all reference types have a similar internal structure, if you try to use a string on an Form instance, it will throw an exception of wrong usage (because it detects a Form is not a string or any of its subtypes).

Also, is the following statamente from C# 4.0 in a Nutshell correct?

Upcasting and downcasting between compatible reference types performs reference
conversions: a new reference is created that points to the same object.

Does it really create a new reference? I thought it’d be the same reference, only stored in a different type of variable.

Thanks

  • 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-14T19:42:26+00:00Added an answer on May 14, 2026 at 7:42 pm

    I had the idea that actually no casting code would be embedded
    in the code itself.

    An interesting idea. How did you imagine that this worked?

    try
    {
        object x = 123;
        object y = (string)x;
    }
    catch(InvalidCastException ex)
    { ... }
    

    If the cast produces no code then where does the code that throws the exception happen?

    Remember, the primary purpose of a cast from a less specific type to a more specific type is to perform a runtime type check.

    Once the type check passes, then sure, nothing else really has to happen. The bits of the reference before the type check and the bits after the type check are the same bits; we’ve just had the runtime verify that the new usage of the old bits is justified.

    if you try to use a string on an Form instance, it will throw an exception of wrong usage (because it detects a Form is not a string or any of its subtypes).

    Where does it detect that? I mean, in exactly which instruction is that detected? In the castclass instruction. That’s what the castclass instruction is for.

    what would happen had I ommited the castclass string line and tried to run the code?

    The type safety verifier would have rejected your program. Had you forced the CLR to run it without passing verification then it would have had undefined behaviour. It might have succeeded, it might have failed, it might have formatted your hard disk.

    Does it really create a new reference?

    Remember, at the implementation level a reference is just a pointer-sized integer. It’s a number that the memory manager can use to track the position of the referred-to data. It might be a pointer, it might be a handle, it doesn’t matter what it is; it’s something that implements the abstract notion of a reference.

    When you have a variable that contains 12 and you “replace” its contents with 12, is that a “new” 12 that has just been created or is it the “old” 12? Suppose you make a second variable and put 12 in it too by copying from the first variable. Is that a “new” 12 or the “old” 12? How can you tell? It’s a difference that makes no difference. When you make a “new” reference that is identical to an “old” reference is that creating something new? The question is a philosophical question, not a technical one.

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

Sidebar

Related Questions

Doing some code reviews lately I came across a number of classes that have
In F# I have a function that returns System.Linq.Expression instances: and System.Object with member
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
Doing some work on controlling printing via the System.Printing framework (main classes are PrintQueue,
Doing some SOAP calls to a 3rd party application. They provide this soap header
Doing cross platform development with 64bit. Using gcc/linux and msvc9/server 2008. Just recently deployed
Doing some refactoring in some legacy code I've found in a project. This is
I'm currently reading Agile Web Development With Rails, 3rd edition. On page 672, I

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.