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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:39:15+00:00 2026-06-01T20:39:15+00:00

I am writing a library that renders a bunch of child objects to screen.

  • 0

I am writing a library that renders a bunch of child objects to screen. The child object is abstract, and it is intended for users of this library to derive their own child from this abstract class.

public abstract class Child : IRenderable {}

public interface IParent<T> where T : Child
{
   IEnumerable<T> Children { get; }
}

The complication is that I do not have a list of IParent to work with, instead, I have a bunch of IRenderables. The user of the library is expected to write something like this:

public class Car : IRenderable { }
public class Cow : IRenderable, IParent<Calf> { }
public class Calf : Child { }

// note this is just an example to get the idea
public static class App
{
   public static void main()
   {
      MyLibraryNameSpace.App app = new MyLibraryNameSpace.App();
      app.AddRenderable(new Car()); // app holds a list of IRenderables
      app.AddRenderable(new Cow());
      app.Draw(); // app draws the IRenderables
   }
}

In Draw(), the library should cast and check whether the IRenderable is also an IParent. However, since I do not know about the Calf, I don’t know what to cast Cow into.

// In Draw()
foreach(var renderable in Renderables)
{
   if((parent = renderable as IParent<???>) != null) // what to do?
   {
      foreach(var child in parent.Children)
      {
          // do something to child here.
      }
   }
}

How can I overcome this problem? Is this anything to do with covariance generics or what-so-ever (I am not familiar with the covariance concept)?

  • 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-01T20:39:17+00:00Added an answer on June 1, 2026 at 8:39 pm

    Since IParent<T> only returns items of type T, you could make it covariant using the out modifier:

    public interface IParent<out T> where T : Child
    {
       IEnumerable<T> Children { get; }
    }
    

    This would make IParent<anything> convertible to IParent<Child>:

    IParent<Child> parent = renderable as IParent<Child>; // works for Cow
    

    Note that covariance only works as long as you are only returning objects of type T (simply speaking). For example, as soon as you add an AddChild(T) method to your IParent interface, covariance must break (= the compiler will complain), since, otherwise, the following type-unsafe code could be written:

    IParent<Child> parent = renderable as IParent<Child>;
    parent.AddChild(new Kitten()); // can't work if parent is really a Cow.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a library that generates derived classes of abstract classes dynamically at
I'm writing a library that implements a distributed object system over a socket connection.
I have a library that handles reading and writing a cache file. This library
Ok I am writing a library that will be shard between unix and windows.
I am writing a library that will provide a collection of public types to
I am writing a library that creates multiple elements inside a wrapper element, and
I'm writing a little library that should be used by people who don't know
So I'm writing a Javascript library that takes x,y coordinates and uses them to
I'm writing a simple Javascript library that makes use of some WebGL code. I'd
I am writing a Win32 DLL library that can be redistributed. I am using

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.