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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:09:39+00:00 2026-05-12T23:09:39+00:00

I have the following extension method to add the elements in one collection to

  • 0

I have the following extension method to add the elements in one collection to another collection:

public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list)
{
    foreach (var item in list)
    {             
        collection.Add(item);
    }
}

This works fine if the IEnumerable list is the same type as the ICollection I’m trying to add it to. However, if I have something like this:

var animals = new List<Animal>();
var dogs = new List<Dog>(); // dog is a subclass of animal
animals.AddRange(dogs); // this line has a compiler error, it can't infer the type

How do I modify my extension method to be able to do something like this, if the type of the IEnumerable is a subclass (or implements the interface) of the T type?

  • 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-12T23:09:39+00:00Added an answer on May 12, 2026 at 11:09 pm

    This method will give you what you want:

    public static void AddRange<A,B>(
      this ICollection<A> collection, 
      IEnumerable<B> list)
        where B: A
    {
        foreach (var item in list)
        {
            collection.Add(item);
        }
    }
    

    A couple of notes:

    1. The only way that this can work is if you use a type for B that is derived from A. For example, Dog is a subclass of Animal, so AddRange will work. This is enforced by the “where B: A” clause. Without this, the ICollection.Add() call will fail due to an incompatible type for B being passed into the ICollection expecting an A.
    2. There’s not a lot of need to restrict the type of A to anything in the Animal type hierarchy; the extension method could be used anywhere you have one type deriving from another.
    3. This isn’t really a problem of the compiler not being able to infer the types. Even if you explicitly passed the types for A and B everywhere, you’ll still get a compiler error.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following extension method public static class ListExtensions { public static IEnumerable<T>
This extension method does not work on two separate development machines: public static string
Update: This does work, I was being stupid :( i have the following extension
I have the following extension method, and would like to make it more generic
I have a lot of code that takes on the following pattern void Add(int
I am trying attempting to refactor the following code: public static void SaveSplitbar(RadSplitBar splitBar)
I have the following extension method which asserts that a property (Id) contains a
In some legacy code I have seen the following extension method to facilitate adding
I have following LINQ statement and I want to rewrite it using extension methods.
I have a prototype model where I need to include the following extension methods

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.