Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About an assignment in implementation of Stack in CoreFX

Tags:

c#

.net

.net-core

In corefx/src/System.Collections/src/System/Collections/Generic/Stack.cs you can find this code:

public T Peek()
{
    int size = _size - 1;
    T[] array = _array;

    if ((uint)size >= (uint)array.Length)
    {
        ThrowForEmptyStack();
    }

    return array[size];
}

The question is why they defined array and not using _array? The same goes in Push() and Pop().

Edit: Not happened in Peek() of Queue.

like image 628
Babak Avatar asked Nov 19 '25 02:11

Babak


1 Answers

The cool thing about GitHub is that you can just look at who, when, and why changes were made.

According to the commit that made this change, it was necessary (at least at that time) to eliminate a bounds check that started happening in .NET Core 2.1.

like image 158
David Browne - Microsoft Avatar answered Nov 21 '25 17:11

David Browne - Microsoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!