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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:44:46+00:00 2026-05-23T03:44:46+00:00

interface Base { … } class Sub : Base { … } class OtherBase<T>

  • 0
interface Base { ... }
class Sub : Base { ... }

class OtherBase<T> where T : Base { ... }
class OtherSub<T> : OtherBase<T> where T : Base { ... }

//...in some class
void Call<T>() where T : OtherBase<Base> { }

//...
Call<OtherSub<Sub>>(); //compile fails...

Seems like when using generics, the compiler won’t cast a inner generic type (Base/Sub) in the
generic type (OtherBase/OtherSub). Why does this happen?

Update:
Please also explain the difference between the above and the following (which works)

void Call<T>() where T : Base { }
//...
Call<Sub>();
  • 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-23T03:44:47+00:00Added an answer on May 23, 2026 at 3:44 am

    Forbidding this behaviour (known as “generic variance”) is necessary because otherwise the following code would compile:

    List<string> strlist = new List<string>();
    List<object> objlist = strlist;
    objlist.Add(42);
    

    We’ve added a number to a list of strings. Not good. (Incidentally, the code would compile for arrays instead of Lists because Java allowed this for some reason; however, this will raise a runtime exception.)

    You can avoid this in your case though:

    static void Call<U, T>(T x) where U : Base where T : OtherBase<U> { }
    

    And call it like this:

    Call(new OtherSub<Sub());
    

    C# 4.0 furthermore provides generic variance for interfaces. However, their use isn’t often necessary.

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

Sidebar

Related Questions

using System; namespace random { interface IHelper { void HelpMeNow(); } public class Base
I'd like to define an interface and abstract base class for a set of
I'd like to find all the types inheriting from a base/interface. Anyone have a
Given a base class with the following interface: public class Base { public virtual
If I implement an interface on a base class will it be inherited by
I have an abstract base class which acts as an interface. I have two
I have an interface with several events I have base class implementing the interface
When should I use an interface and when should I use a base class?
I am trying to provide an interface to an abstract generic base class. I
Given this: Interface IBase {string X {get;set;}} Interface ISuper {string Y {get;set;}} class Base

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.