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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:19:08+00:00 2026-05-15T22:19:08+00:00

I have two methods with these signatures: void Method<T>(T data) { } void Method<T>(IEnumerable<T>

  • 0

I have two methods with these signatures:

void Method<T>(T data)
{
}

void Method<T>(IEnumerable<T> data)
{
}

Its an overload of the same method to take either a single object or a list of them. If I try to pass a List<‘T> to it, it resolves to the first method, when obviosly i want the second. I have to use list.AsEnumerable() to get it to resolve to the second. Is there any way to make it resolve to the second regardless of whether the list is type T[], IList<‘T>, List<‘T>, Collection<‘T>, IEnumerable<‘T>, etc.

  • 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-15T22:19:09+00:00Added an answer on May 15, 2026 at 10:19 pm

    The best solution: do not go there in the first place. This is a bad design. You’ll note that none of the framework classes do this. A list has two methods: Add, and AddRange. The first adds a single item, the second adds a sequence of items.

    It’s a bad design because you are writing a device for automatically producing bugs. Consider again the example of a mutable list:

    List<object> myqueries = new List<object>();
    myqueries.Add(from c in customers select c.Name);
    myqueries.Add(from o in orders where o.Amount > 10000.00m select o);
    

    You’d expect that to add two queries to the list of queries; if Add were overloaded to take a sequence then this would add the query results to the list, not the queries. You need to be able to distinguish between a query and its results; they are logically completely different.

    The best thing to do is to make the methods have two different names.

    If you’re hell bent on this bad design, then if it hurts when you do that, don’t do that. Overload resolution is designed to find the best possible match. Don’t try to hammer on overload resolution so that it does something worse. Again, that is confusing and bug prone. Solve the problem using some other mechanism. For example:

    static void Frob(IEnumerable ts) // not generic!
    {
        foreach(object t in ts) Frob<object>(t);
    }
    static void Frob<T>(T t)
    {
        if (t is IEnumerable)
            Frob((IEnumerable) t);
        else
            // otherwise, frob a single T.
    }
    

    Now no matter what the user gives you – an array of T, an array of lists of T, whatever, you end up only frobbing single Ts.

    But again, this is almost certainly a bad idea. Don’t do it. Two methods that have different semantics should have different names.

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

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this seems to be a known bug in netbeans. Some… May 16, 2026 at 4:37 am
  • Editorial Team
    Editorial Team added an answer You can show a display a modal view controller from… May 16, 2026 at 4:37 am
  • Editorial Team
    Editorial Team added an answer If the two FTP classes are part of a third-party… May 16, 2026 at 4:36 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.