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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:29:24+00:00 2026-06-15T19:29:24+00:00

ReSharper suggested to enumerate an IEnumerable<T> to a list or array since I had

  • 0

ReSharper suggested to enumerate an IEnumerable<T> to a list or array since I had “possible multiple enumerations of IEnumerable<T>“.

The automatic code re-factoring suggested has some optimization built in to see whether IEnumerable<T> already is an array before calling ToArray().

var list = source as T[] ?? source.ToArray();
  • Isn’t this optimization already built-in the original LINQ method?
  • If not, what would be the motivation not to do so?
  • 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-15T19:29:25+00:00Added an answer on June 15, 2026 at 7:29 pm

    Nope, there is no such optimization. If source is ICollection, then it will be copied to new array. Here is code of Buffer<T> struct, which used by Enumerable to create array:

    internal Buffer(IEnumerable<TElement> source)
    {    
        TElement[] array = null;
        int length = 0;
        ICollection<TElement> is2 = source as ICollection<TElement>;
        if (is2 != null)
        {
             length = is2.Count;
             if (length > 0)
             {
                 array = new TElement[length]; // create new array
                 is2.CopyTo(array, 0); // copy items
             }
        }
        else // we don't care, because array is ICollection<TElement>
    
        this.items = array;
    }
    

    And here is Enumerable.ToArray() method:

    public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)
    {
        if (source == null)
        {
            throw Error.ArgumentNull("source");
        }
        Buffer<TSource> buffer = new Buffer<TSource>(source);
        return buffer.ToArray(); // returns items
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Resharper has suggested to change from interface IModelMapper<TFrom, TTo> { TTo Map(TFrom input); }
As I am typing certain functions or variables, Resharper suggests a possible list (Intellisense)
Resharper is showing a Possible System.NullReferenceException warning. I however can't see how I can
Resharper (paired with StyleCop) has made me a bit of a neat freak when
Possible Duplicate: Resharper: vars Is there a reason that resharper suggests var thing1 =
When I ran ReSharper on my code, for example: if (some condition) { Some
I had this code: string[] args = Environment.GetCommandLineArgs(); bool grabNext; foreach (string arg in
In my controller code I am using: Request.Url.<Something> . ReSharper suggests that Request.Url can
I worked with Resharper 5 before and when I have a problem in code
I was working on refactoring some c# code with ReSharper. One of the things

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.