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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:16:50+00:00 2026-06-11T01:16:50+00:00

If I try an invalid cast from a class to an interface, then the

  • 0

If I try an invalid cast from a class to an interface, then the compiler doesn’t complain (the error occurs at runtime); it does complain, however, if I try a similar cast to an abstract class.

class Program
{
    abstract class aBaz
    {
        public abstract int A { get; }
    }

    interface IBar
    {
        int B { get; }
    }

    class Foo
    {
        public int C { get; }
    }

    static void Main()
    {
        Foo foo = new Foo();

        // compiler error, as expected, since Foo doesn't inherit aBaz
        aBaz baz = (aBaz)foo;

        // no compiler error, even though Foo doesn't implement IBar
        IBar bar = (IBar)foo;
    }
}

Why doesn’t the compiler reject the cast from Foo to IBar, when it’s (seemingly?) invalid? Or, to flip the question, if the compiler allows this “invalid” cast to the interface IBar, why doesn’t it allow the similar “invalid” cast to the abstract class aBaz?

  • 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-06-11T01:16:52+00:00Added an answer on June 11, 2026 at 1:16 am

    You need to understand the inheritance system of .Net to see why this makes sense. In .Net, a class may inherit from only one base class but may implement any number of interfaces.

    class Program
    {
        abstract class aBaz
        {
            public abstract int A { get; }
        }
    
        interface IBar
        {
            int B { get; }
        }
    
        class Foo
        {
            public int C { get; }
        }
    
        class BarableFoo : Foo, IBar
        {
            public int C { get; }
        }
    
        static void Main()
        {
            // This is why the compiler doesn't error on the later cast
            Foo foo = new BarableFoo();
    
            // compiler error: aBaz is a class and the compiler knows that
            // Foo is not a _subclass_ of aBaz.
            aBaz baz = (aBaz)foo;
    
            // no compiler error: the class Foo does not implement IBar, however at runtime
            // this instance, "foo", might be a subclass of Foo that _implements_ IBar.
            // This is perfectly valid, and succeeds at runtime.
            IBar bar = (IBar)foo;
    
            // On the other hand...
            foo = new Foo();
    
            // This fails at runtime as expected. 
            bar = (IBar)foo;
        }
    
    }
    

    In the extremely simple original example in the question, it seems like the compiler could detect that this instance of foo is never going to be castable to IBar, but that is more of a “nice to have” warning than a matter of language correctness.

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

Sidebar

Related Questions

I got the error invalid cast exception unhandled. I'm using SQL Server 2008 and
I'm getting an 'UnauthorizedAccesExpection - Invalid cross-thread access' exception when I try to raise
When I try to put a value into a DATE field which is invalid,
I get the following exception java.lang.NumberFormatException: Invalid int: It is happening when you try
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
try: html = urlopen('http://glbse.com/api/asset/' + asset.name) except: print 'error while updating the price of
All objects in my program inherit from a Container class. The Container class has
How come I get an invalid cast exception when trying to set a NULL
Why would the cast (to a System.Guid type) statement be invalid (second line in

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.