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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:25:56+00:00 2026-05-29T16:25:56+00:00

IEnumerable does not guarantee that enumerating twice will yield the same result. In fact,

  • 0

IEnumerable does not guarantee that enumerating twice will yield the same result. In fact, it’s quite easy to create an example where myEnumerable.First() returns different values when executed twice:

class A {
    public A(string value) { Value = value; }
    public string Value {get; set; } 
}

static IEnumerable<A> getFixedIEnumerable() {
    return new A[] { new A("Hello"), new A("World") };
}

static IEnumerable<A> getDynamicIEnumerable() {
    yield return new A("Hello");
    yield return new A("World");
}

static void Main(string[] args)
{
    IEnumerable<A> fix = getFixedIEnumerable();
    IEnumerable<A> dyn = getDynamicIEnumerable();

    Console.WriteLine(fix.First() == fix.First()); // true
    Console.WriteLine(dyn.First() == dyn.First()); // false
}

This is not just an academic example: Using the popular from ... in ... select new A(...) will create exactly this situation. This can lead to unexpected behaviour:

fix.First().Value = "NEW";
Console.WriteLine(fix.First().Value); // prints NEW

dyn.First().Value = "NEW";
Console.WriteLine(dyn.First().Value); // prints Hello

I understand why this happens. I also know that this could be fixed by executing ToList() on the Enumerable or by overriding == for class A. That’s not my question.

The question is: When you write a method that takes an arbitrary IEnumerable and you want the property that the sequence is only evaluated once (and then the references are “fixed”), what’s the canonical way to do this? ToList() seems to be used mostly, but if the source is fixed already (for example, if the source is an array), the references are copied to a list (unnecessarily, since all I need is the fixed property). Is there something more suitable or is ToList() the “canonical” solution for this issue?

  • 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-29T16:25:56+00:00Added an answer on May 29, 2026 at 4:25 pm

    ToList is very definitely the way to go.

    It has some optimisations: if the input enumeration is an ICollection (e.g. an array or list) it will call ICollection.CopyTo to copy the collection to an array rather than actually enumerating – so the cost is unlikely to be significant unless the collection is enormous.

    IMHO, in general it is better for most methods to return ICollection<T> or IList<T> rather than IEnumerable<T>, unless you want to hint to consumers of the method that the implementation may use lazy evaluation (e.g. yield).

    In cases where a method should return an immutable list, return a readonly wrapper (ReadOnlyCollection<T>) e.g. by calling ToList().AsReadOnly(), but still return the interface type IList<T>.

    If you follow this guideline, consumers of the method won’t ever need an unnecessary call to ToList.

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

Sidebar

Related Questions

I have just noticed that a multidimensional array in C# does not implement IEnumerable<T>
I want to create an object with a non-enumerable property(a property that does not
When I make the same query twice, the second time it does not return
Since IEnumerable.Contains() method does not accept a predicate as an argument, Most people use
Did anyone ever manage to create forms within the grid? Attempt (which does not
I tested this block of code and find that the GetInts method does not
I would like to create a method that orders an IEnumerable List by a
If I have two yield return methods with the same signature, the compiler does
I'm surprised to see that there does not appear to be a method to
why linq's except extension method does not have Except<TSource> Method (IEnumerable<TSource>,HashSet<TSource>) overload? for example

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.