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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:50:15+00:00 2026-05-16T10:50:15+00:00

I am having a problem understanding how polymorphism works when using generics. As an

  • 0

I am having a problem understanding how polymorphism works when using generics. As an example, I have defined the following program:

public interface IMyInterface
{
    void MyMethod();
}

public class MyClass : IMyInterface
{
    public void MyMethod()
    {
    }
}

public class MyContainer<T> where T : IMyInterface
{
    public IList<T> Contents;
}

I can then do this, which works just fine:

MyContainer<MyClass> container = new MyContainer<MyClass>();
container.Contents.Add(new MyClass());

I have many classes that implement MyInterface. I would like to write a method that can accept all MyContainer objects:

public void CallAllMethodsInContainer(MyContainer<IMyInterface> container)
{
    foreach (IMyInterface myClass in container.Contents)
    {
        myClass.MyMethod();
    }
}

Now, I’d like to call this method.

MyContainer<MyClass> container = new MyContainer<MyClass>();
container.Contents.Add(new MyClass());
this.CallAllMethodsInContainer(container);

That didn’t work. Surely, because MyClass implements IMyInterface, I should be able to just cast it?

MyContainer<IMyInterface> newContainer = (MyContainer<IMyInterface>)container;

That didn’t work either. I can definitely cast a normal MyClass to IMyInterface:

MyClass newClass = new MyClass();
IMyInterface myInterface = (IMyInterface)newClass;

So, at least I haven’t completely misunderstood that. I am unsure exactly how I am to write a method that accepts a generic collection of classes that conform to the same interface.

I have a plan to completely hack around this problem if need be, but I would really prefer to do it properly.

Thank you in advance.

  • 1 1 Answer
  • 1 View
  • 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-16T10:50:15+00:00Added an answer on May 16, 2026 at 10:50 am

    Note: In all cases, you will have to initialize the Contents field to a concrete object that implements IList<?>

    When you keep the generic constraint, you can do:

    public IList<T> Contents = new List<T>();
    

    When you don’t, you can do:

    public IList<MyInterface> Contents = new List<MyInterface>();
    

    Method 1:

    Change the method to:

    public void CallAllMethodsInContainer<T>(MyContainer<T> container) where T : IMyInterface
    {
        foreach (T myClass in container.Contents)
        {
            myClass.MyMethod();
        }
    }
    

    and the snippet to:

    MyContainer<MyClass> container = new MyContainer<MyClass>();
    container.Contents.Add(new MyClass());
    this.CallAllMethodsInContainer(container);
    

    Method 2:

    Alternatively, move the CallAllMethodsInContainer method to the MyContainer<T> class like this:

    public void CallAllMyMethodsInContents()
        {
            foreach (T myClass in Contents)
            {
                myClass.MyMethod();
            }
        }
    

    and change the snippet to:

    MyContainer<MyClass> container = new MyContainer<MyClass>();
    container.Contents.Add(new MyClass());
    container.CallAllMyMethodsInContents();
    

    Method 3:

    EDIT: Yet another alternative is to remove the generic constraint from the MyContainer class like this:

    public class MyContainer
    {
        public IList<MyInterface> Contents;
    }
    

    and to change the method signature to

      public void CallAllMethodsInContainer(MyContainer container)
    

    Then the snippet should work as:

    MyContainer container = new MyContainer();
    container.Contents.Add(new MyClass());
    this.CallAllMethodsInContainer(container);
    

    Note that with this alternative, the container’s Contents list will accept any combination of objects that implement MyInterface.

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

Sidebar

Related Questions

I'm having problems understanding how latest.integration works. I have an example that is not
I'm having a problem understanding Java generics and I've simplified to this example class
I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following
I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence
I'm having a problem understanding what I'm doing wrong here. I have the code
I was goint through k & r. I was having problem in understanding following
I'm having problem with understanding some of QList behavior. #include <QList> #include <iostream> using
I'm having a huge problem in understanding Membership with MVC. We have in our
I'm having a problem understanding how to do something in LINQ. I have a
I am having a problem in understanding the security issues with the following scenario.

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.