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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With