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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:30:37+00:00 2026-06-04T19:30:37+00:00

I stumbled across this odd case yesterday, where t as D returns a non-null

  • 0

I stumbled across this odd case yesterday, where t as D returns a non-null value, but (D)t causes a compiler error.

Since I was in a hurry I just used t as D and carried on, but I am curious about why the cast is invalid, as t really is a D. Can anyone shed some light on why the compiler doesn’t like the cast?

class Program
{
    public class B<T> where T : B<T> { }

    public class D : B<D> { public void M() { Console.Out.WriteLine("D.M called."); } }

    static void Main() { M(new D()); }

    public static void M<T>(T t) where T : B<T>
    {
        // Works as expected: prints "D.M called."
        var d = t as D;
        if (d != null)
            d.M();

        // Compiler error: "Cannot cast expression of type 'T' to type 'D'."
        // even though t really is a D!
        if (t is D)
            ((D)t).M();
    }
}

EDIT: Playing around, I think this is a clearer example. In both cases t is constrained to be a B and is maybe a D. But the case with the generic won’t compile. Does the C# just ignore the generic constraint when determining if the cast is legal? Even if it does ignore it, t could still be a D; so why is this a compile time error instead of a runtime exception?

class Program2
{
    public class B { }

    public class D : B { public void M() { } }

    static void Main()
    {
        M(new D());
    }

    public static void M(B t)
    {
        // Works fine!
        if (t is D)
            ((D)t).M();
    }

    public static void M<T>(T t) where T : B
    {
        // Compile error!
        if (t is D)
            ((D)t).M();
    }
}
  • 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-04T19:30:38+00:00Added an answer on June 4, 2026 at 7:30 pm

    In your second example you can change

    ((D)t).M();
    

    to

    ((D)((B)t)).M();
    

    You can cast from B to D, but you can’t necessarily cast from “something that is a B” to D. “Something that is a B” could be an A, for example, if A : B.

    The compiler error comes about when you are potentially jumping from child to child in the hierarchy. You can cast up and down the hierarchy, but you can’t cast across it.

    ((D)t).M();       // potentially across, if t is an A
    ((D)((B)t)).M();  // first up the hierarchy, then back down
    

    Notice also that you might still get a runtime error with ((D)((B)t)).M(); if t is not actually a D. (your is check should prevent this though.)

    (Also notice that in neither case is the compiler taking into account the if (t is D) check.)

    A last example:

    class Base { }
    class A : Base { }
    class C : Base { }
    
    ...
    A a = new A();
    C c1 = (C)a;         // compiler error
    C c2 = (C)((Base)a); // no compiler error, but a runtime error (and a resharper warning)
    
    // the same is true for 'as'
    C c3 = a as C;           // compiler error
    C c4 = (a as Base) as C; // no compiler error, but always evaluates to null (and a resharper warning)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I stumbled across this when doing a refactoring with Grails 2.0.1 but I pulled
While compiling a demo from openni i stumbled across this error: Exception in thread
I am using EF 4.0 and POCO's. I stumbled across this error while inserting
I stumbled across this while starting to learn about vars here: http://msdn.microsoft.com/en-us/library/bb384061.aspx However, I
I stumbled across this problem in F#. Suppose, I want to declare two types
I stumbled across this C code today. Can anyone tell me what the 'where'
I have stumbled across this insane behavior where image.onload gets fired multiple times instead
I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I need to add indexes to my table (columns) and stumbled across this post:
So, poking through the n869 draft of the C99 standard I stumbled across this

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.