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 426763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:30:43+00:00 2026-05-12T19:30:43+00:00

When I compile the following code, I only see the error during Run-time which

  • 0

When I compile the following code, I only see the error during Run-time which says
“Unable to cast object of type ‘Foo1’ to type ‘Foo2′”

Why is the compiler not showing this error during compile time?

public void Start()
{
     Foo1 objFoo1 = new Foo1();
     Foo2 objFoo2 = (Foo2)objFoo1;

     //objFoo1.FooA = 10;
     //Console.WriteLine(objFoo2.FooA);

}

public class Foo1    {}
public class Foo2 : Foo1    {}
  • 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-12T19:30:43+00:00Added an answer on May 12, 2026 at 7:30 pm

    The compiler is not smart enough to know that objFoo1 is really a simple Foo1. It doesn’t analyze your source code deeply enough to determine that. All it sees is that objFoo1 is a Foo1, Foo2 is derived from Foo1, and therefore the cast is legal. If instead you changed that to (int) objFoo1 it would bomb at compile-time since the compiler could see that there’s no possible way to turn a Foo1 into an integer.

    Imagine that there were 1000 lines of code between the two declarations, many of them doing various assignments to objFoo1. The compiler would have to do a lot of hard work to attempt to determine what object exactly is in objFoo1. In general it wouldn’t always be able to do so. It’s far easier for the compiler to just take the static type information at face value and assume that objFoo1 is some type of Foo1 object but no more. That way it doesn’t reject your simple test program but accept a more complicated one.

    It’s also not the compiler’s job to reject your code, in this case. It is possible, though admittedly far-fetched, that you are intentionally trying to generate a ClassCastException by doing an illegal cast. It’d be unusual but not illegal to do that.

    Along these lines, there are other cases where the compiler’s static code analysis can be fooled. This will fail to compile:

    return;
    System.out.println("hello world!"); // compile error - unreachable code
    

    While this will compile fine even though it is semantically identical:

    if (true) return;
    System.out.println("hello world!");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

I'm having trouble getting hello world going with eclipseFP and Haskell. I have the
I'm currently working on cleaning up an API full of function templates, and had
EDIT See my solution below /EDIT I have a Visual Studio solution with two
My compiler behaves oddly when I try to pass a fixed-size array to a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.