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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:37:51+00:00 2026-05-30T00:37:51+00:00

Some years back, somebody complained about the implementation of Linq.Reverse() and Microsoft promised to

  • 0

Some years back, somebody complained about the implementation of Linq.Reverse() and Microsoft promised to fix that. This was in 2008, so the question is, does Framework 4 have an optimized implementation of Linq.Reverse() that does not materialize the collection (i.e. copy all elements to an internal array) when the collection type allows it (e.g. IList<T>)?

  • 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-30T00:37:52+00:00Added an answer on May 30, 2026 at 12:37 am

    Obviously it’s not possible to optimize all cases. If some object implements only IEnumerable<T> and not IList<T>, you have to iterate it until the end to find the last element. So the optimization would be only for types that implement IList<T> (like T[] or List<T>).

    Now, is it actually optimized in .Net 4.5 DP? Let’s fire up Reflector ILSpy:

    public static IEnumerable<TSource> Reverse<TSource>(
        this IEnumerable<TSource> source)
    {
        if (source == null)
        {
            throw Error.ArgumentNull("source");
        }
        return ReverseIterator<TSource>(source);
    }
    

    Okay, how does ReverseIterator<TSource>() look?

    private static IEnumerable<TSource> ReverseIterator<TSource>(
        IEnumerable<TSource> source)
    {
        Buffer<TSource> buffer = new Buffer<TSource>(source);
        for (int i = buffer.count - 1; i >= 0; i--)
        {
            yield return buffer.items[i];
        }
        yield break;
    }
    

    What that iterator block does is to create a Buffer<T> for the collection and iterate backwards through that. We’re almost there, what’s Buffer<T>?

    [StructLayout(LayoutKind.Sequential)]
    internal struct Buffer<TElement>
    {
        internal TElement[] items;
        internal int count;
        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];
                    is2.CopyTo(array, 0);
                }
            }
            else
            {
                foreach (TElement local in source)
                {
                    if (array == null)
                    {
                        array = new TElement[4];
                    }
                    else if (array.Length == length)
                    {
                        TElement[] destinationArray = new TElement[length * 2];
                        Array.Copy(array, 0, destinationArray, 0, length);
                        array = destinationArray;
                    }
                    array[length] = local;
                    length++;
                }
            }
            this.items = array;
            this.count = length;
        }
    
        // one more member omitted
    }
    

    What have we here? We copy the content to an array. In every case. The only optimization is that if we know Count (that is, the collection implements ICollection<T>), we don’t have to reallocate the array.

    So, the optimization for IList<T> is not in .Net 4.5 DP. It creates a copy of the whole collection in every case.

    If I were to guess why it isn’t optimized, after reading Jon Skeet’s article on this issue, I think it’s because that optimization is observable. If you mutate the collection while iterating, you would see the changed data with the optimization, but the old data without it. And optimizations that actually change behavior of something in subtle ways are a bad thing, because of backwards compatibility.

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

Sidebar

Related Questions

I asked a question about this some years back when Vista was first released,
I had this problem some years ago and back then I implemented a "different
I remember using a program, some years back, that allowed me to fine-tune my
Some 4 years back, I followed this MSDN article for DateTime usage best practices
Some years back, I use to do something similar to this on top of
I've been developing with PHP for some years now, and recently came across this
I have about 4-5 years of background in programming some in C# and some
So a couple years back I took some time to grok make , and
I used DirectDraw in C and C++ years back to draw some simple 2D
I have an existing MVC site, built some years back, and I am now

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.