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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:56:42+00:00 2026-05-22T11:56:42+00:00

This got a bit long-winded, so here’s the quick version: Why does this cause

  • 0

This got a bit long-winded, so here’s the quick version:

Why does this cause a runtime TypeLoadException? (And should the compiler prevent me from doing it?)

interface I
{
    void Foo<T>();
}

class C<T1>
{
    public void Foo<T2>() where T2 : T1 { }
}

class D : C<System.Object>, I { } 

The exception occurs if you try to instantiate D.


Longer, more exploratory version:

Consider:

interface I
{
    void Foo<T>();
}

class C<T1>
{
    public void Foo<T2>() where T2 : T1 { }
}

class some_other_class { }

class D : C<some_other_class>, I { } // compiler error CS0425

This is illegal because the type constraints on C.Foo() don’t match those on I.Foo(). It generates compiler error CS0425.

But I thought I might be able to break the rule:

class D : C<System.Object>, I { } // yep, it compiles

By using Object as the constraint on T2, I’m negating that constraint. I can safely pass any type to D.Foo<T>(), because everything derives from Object.

Even so, I still expected to get a compiler error. In a C# language sense, it violates the rule that “the constraints on C.Foo() must match the constraints on I.Foo()”, and I thought the compiler would be a stickler for the rules. But it does compile. It seems the compiler sees what I’m doing, comprehends that it’s safe, and turns a blind eye.

I thought I’d gotten away with it, but the runtime says not so fast. If I try to create an instance of D, I get a TypeLoadException: “Method ‘C`1.Foo’ on type ‘D’ tried to implicitly implement an interface method with weaker type parameter constraints.”

But isn’t that error technically wrong? Doesn’t using Object for C<T1> negate the constraint on C.Foo(), thereby making it equivalent to – NOT stronger than – I.Foo()? The compiler seems to agree, but the runtime doesn’t.

To prove my point, I simplified it by taking D out of the equation:

interface I<T1>
{
    void Foo<T2>() where T2 : T1;
}

class some_other_class { }

class C : I<some_other_class> // compiler error CS0425
{
    public void Foo<T>() { }
}

But:

class C : I<Object> // compiles
{
    public void Foo<T>() { }
}

This compiles and runs perfectly for any type passed to Foo<T>().

Why? Is there a bug in the runtime, or (more likely) is there a reason for this exception that I’m not seeing – in which case shouldn’t the compiler have stopped me?

Interestingly, if the scenario is reversed by moving the constraint from the class to the interface…

interface I<T1>
{
    void Foo<T2>() where T2 : T1;
}

class C
{
    public void Foo<T>() { }
}

class some_other_class { }

class D : C, I<some_other_class> { } // compiler error CS0425, as expected

And again I negate the constraint:

class D : C, I<System.Object> { } // compiles

This time it runs fine!

D d := new D();
d.Foo<Int32>();
d.Foo<String>();
d.Foo<Enum>();
d.Foo<IAppDomainSetup>();
d.Foo<InvalidCastException>();

Anything goes, and that makes perfect sense to me. (Same with or without D in the equation)

So why does the first way break?

Addendum:

I forgot to add that there is a simple workaround for the TypeLoadException:

interface I
{
    void Foo<T>();
}

class C<T1>
{
    public void Foo<T2>() where T2 : T1 { }
}

class D : C<Object>, I 
{
    void I.Foo<T>() 
    {
        Foo<T>();
    }
}

Explicitly implementing I.Foo() is fine. Only the implicit implementation causes the TypeLoadException. Now I can do this:

        I d = new D();
        d.Foo<any_type_i_like>();

But it’s still a special case. Try using anything else other than System.Object, and this won’t compile. I feel a bit dirty doing this because I’m not sure if it intentionally works this way.

  • 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-22T11:56:43+00:00Added an answer on May 22, 2026 at 11:56 am

    It’s a bug – see Implementing Generic Method From Generic Interface Causes TypeLoadException and Unverifiable Code with Generic Interface and Generic Method with Type Parameter Constraint. It’s not clear to me whether it’s a C# bug or a CLR bug, though.

    [Added by OP:]

    Here’s what Microsoft says in the second thread you linked to (my emphasis):

    There is a mismatch between the
    algorithms used by the runtime and the
    C# compiler to determine if one set of
    constraints is as strong as another
    set. This mismatch results in the C#
    compiler accepting some constructs
    that the runtime rejects and the
    result is the TypeLoadException you
    see. We are investigating to determine
    if this code is a manifestation of
    that problem. Regardless, it is
    certainly not “By Design” that the
    compiler accepts code like this
    that
    results in a runtime exception.

    Regards,

    Ed Maurer C# Compiler Development
    Lead

    From the part I bolded, I think he’s saying this is a compiler bug. That was back in 2007. I guess it’s not serious enough to be a priority for them to fix it.

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

Sidebar

Related Questions

How can I fetch images from a server? I've got this bit of code
I've got a select with two 'special' options, a bit like this: <select> <option
I am writing my thesis in LaTeX and because things got a bit long
This has got to be something I just missed, but how do I add
This has got to be something simple: I set up a frames page with
I got this error today when trying to open a Visual Studio 2008 project
I got this output when running sudo cpan Scalar::Util::Numeric jmm@freekbox:~/bfwsandbox/sa/angel/astroportal/dtu8e/resources$ sudo cpan Scalar::Util::Numeric [sudo]
I got this síngleton cache object and it exposes an IEnumerable property which just
I got this flash application where you can click a link while watching a
I got this error when I tried to compile an application that includes the

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.