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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:30:09+00:00 2026-06-09T06:30:09+00:00

Works private void Add<H>(H toAdd, IList<dynamic> list) { list.Add(toAdd); } Doesn’t work private void

  • 0

Works

private void Add<H>(H toAdd, IList<dynamic> list)
{
     list.Add(toAdd);
}

Doesn’t work

private void Add<H>(IList<H> toAdd, IList<IList<dynamic>> list)
{
     list.Add(toAdd);
}

As you can imagine, the error is

The best overloaded method match for 'System.Collections.Generic.ICollection<System.Collections.Generic.IList<dynamic>>.Add(System.Collections.Generic.IList<dynamic>)' has some invalid arguments

If anyone knows why this is happening or even better, how to fix it, I am very curious. I assume it has to do with Generic Variance but the dynamic makes me less sure.

Thanks, Tom

Edit

   //doesn't work
   private void Add<H>(IList<H> toAdd, IList<IList<dynamic>> list) where H : object
   {
      list.Add(toAdd);
   }

   //works
   //this isn't good enough however because I only want to be able to
   //have one type of object in toAdd
   private void Add(IList<object> toAdd, IList<IList<dynamic>> list)
   {
      list.Add(toAdd);
   }

   //works
   private void Add<H>(IList<H> toAdd, IList<IList<dynamic>> list)
   {
      list.Add(toAdd.Cast<dynamic>().ToList());
   }

   private void Foo()
   {
      //works
      IList<dynamic> list1 = new List<object>();
      //works
      IList<object> list2 = new List<dynamic>();
      //works
      IList<IList<dynamic>> list4 = new List<IList<object>>();
      //works
      IList<IList<object>> list3 = new List<IList<dynamic>>();
   }

I added a couple more examples (not all surprising) just to illustrate

  • 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-09T06:30:10+00:00Added an answer on June 9, 2026 at 6:30 am

    That’s because you cannot cast an IList<H> to an IList<dynamic>. Imagine what happens:

    IList<H> myList = new List<H>(...);
    IList<dynamic> myDynamicList = myList; // assuming this would compile
    myDynamicList.Add(new Foo()); // boom
    

    You can try to keep a list of IEnumerable<dynamic> instead of IList<dynamic>, since IEnumerable<out T> has a covariant type parameter.

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

Sidebar

Related Questions

I can't figure out why this simple update command won't work: private void button1_Click(object
the code below works in Windows Phone 7 private void ShowTime() { txtTime.Text =
I have simple code witch works ok: public class Example { private List<SomeClass> _list
I'm getting an error when trying to add generic object to a List<>. Its
private void refineWords() { for(String word : words){ Log.i(word, word); if (word == s
// sample 1 use BackgroundWorker, it works fine private BackgroundWorker workerPorcee = new BackgroundWorker();
This works: class ButtonCountObserver { private var cnt = 0 // private field def
The following code works fine. class A { private: int _value; public: class AProxy
I'm trying to animate a private variable named radius, which works. However while its
The following code works & runs perfectly. public class Complex { private int real,

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.