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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:46:43+00:00 2026-06-04T05:46:43+00:00

I have a generic interface hierarchy which describes some controllers for other generic types,

  • 0

I have a generic interface hierarchy which describes some controllers for other generic types, and I’m having trouble clarifying in my mind why a particular casting scenario is not valid.

Simplified code is as follows;

// 'DTO' interfaces
public interface IBase
{ }

public interface IDerived : IBase
{ }


// 'DTOs'
public class Base : IBase
{ }

public class Derived : Base, IDerived
{ }


// controller interfaces
public interface IBaseController<T> where T : class, IBase
{ }

public interface IDerivedController : IBaseController<IDerived>
{ }


// controllers
public class BaseController<T> : IBaseController<T>
    where T : class, IBase
{ }

public class DerivedController : BaseController<IDerived>, IDerivedController
{ }

Now, the situation I’m having trouble with is this;

IDerivedController x = new DerivedController();
bool is1 = x is IDerivedController;   // true
bool is2 = x is IBaseController<IDerived>; // true
bool is3 = x is IBaseController<IBase>; // false ???

The final line is where my confusion lies. The controller interfaces associate correctly, and the ‘DTOs’. But not both together… ?

  • 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-04T05:46:45+00:00Added an answer on June 4, 2026 at 5:46 am

    See http://msdn.microsoft.com/en-us/library/dd997386 for info on variant generic interfaces, which is the heart of what you’re asking. You must declare that an IBaseController<IDerived> can in fact be used as an IBaseController<IBase> in order for it to be used as such.

    For the reason for this, consider trying to use a List<string> as an IList<object> vs trying to use an IEnumerable<string> as an IEnumerable<object>. The IEnumerable conversion is ok because you can use the items as objects without any problems, but you can’t add any object to a List<string>, because then it would contain something besides strings.

    If you change it to public interface IBaseController<out T> where T : class, IBase, then x is IBaseController<IBase> becomes true. If the compiler complains that you can’t make T an out parameter, then you can’t do the conversion you’re looking to do (unless you change the interface to make it compatible).

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

Sidebar

Related Questions

I have a generic interface IConstrained which is implemented by the generic Constrained class.
Let me give example: I have some generic class/interface definition: interface IGenericCar< T >
Let's say I have some generic interface IMyInterface<TParameter1, TParameter2> . Now, if I'm writing
Basically, what I am trying to do is have a generic simulator-interface which acts
I have an interface ISerializeDeserialize defined and some classes inheriting the generic interface. I
Lets say I have a class, which implements a generic interface public interface IItem
I have made a generic Observer interface and an Observable class, but can't compile
I want to have a generic object that implements an interface. I mean if
I'm trying to implement a generic java interface in scala. I have looked at:
I am currently working with .Net 2.0 and have an interface whose generic type

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.