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

  • Home
  • SEARCH
  • 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 7743923
In Process

The Archive Base Latest Questions

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

Previously I used IEnumerable<T> type, if I passed collection as parameter of method. But

  • 0

Previously I used IEnumerable<T> type, if I passed collection as parameter of method.

But recently, I had a problem with the collection of type IEnumerable<T> that was created in similar way:

var peoples = names.Select(name => new People(name));

In this case, always, if I use a collection peoples (for example, foreach), it creates new instance of class People, and it can easily cause an error.

So I want to ask whether it is right to use the IEnumerable <T> type parameter. I think it may cause problems (see example above) and this type should not be used. What alternatives do you recommend (ICollection<T>, IList<T> etc.) and when to use which alternative?

Or do you think that this is a silly question, because the creation of objects in the Select method uses only a fool?

Of course, I know that I can use ToArray() or ToList() and thus solve the problem. But someone else who uses this method, it can not know. I would like to know how to prevent this by selecting the correct type parameter. List or array is too specific for me when I want to just “enumerate” objects.

  • 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-01T09:39:32+00:00Added an answer on June 1, 2026 at 9:39 am

    IEnumerable is not a collection. It is just something that you can “enumerate”. The problem is not passing IEnumerable to your method, the problem is that if you are using LINQ (Select method), every time you read the enumerable it will execute the code again. If you only want to have it executed once, you can use the ToArray() or ToList() methods:

    var peoples = names.Select(name => new People(name)).ToList();
    

    Like this you can still pass it to any method accepting an IEnumerable (or List) and it will only create one instance for each person.

    Edit:
    Your method shouldn’t worry about these kind of problems. It’s the callers problem. There might be perfectly good reasons to call your method with an enumerable instead of a list. The caller should know that the enumerable gives different results if he passes it to different methods, so you shouldn’t worry about that.

    The only exception is if you enumerate the parameter more than once in the method itself. In this case you should cache the parameter in a list inside the method and then enumerate the list instead as many times as you need.

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

Sidebar

Related Questions

I have created a Gemfile in my rack application (that previously used .gems). When
I'm looking into development standards fro JavaScript. I had previously used Doulgas Crockfords Javascript
The only type of 'remote desktop' application I've used previously is VNC viewer. My
I used this code previously in netbeans 6.9.1 but it does not seem to
Previously, I used to use MFC collection classes such CArray and CMap . After
I previously used bash on ubuntu 10.04 LTS as my default shell environment. Recently
i previously used <jsp:useBean> tags that have facility to decide the scope of bean
I've previously used log4net, but my current employer uses Enterprise Library application blocks. I
I've previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want
We previously used many submodules in our main repositories, but to increase the maintainability

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.