Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a945cdd

Browse files
jamesqostephentoub
authored andcommitted
Reduce code size of Stack.Peek (#12094)
1 parent c7c9818 commit a945cdd

File tree

1 file changed

+14
-2
lines changed
  • src/System.Collections/src/System/Collections/Generic

1 file changed

+14
-2
lines changed

src/System.Collections/src/System/Collections/Generic/Stack.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ public void TrimExcess()
196196
public T Peek()
197197
{
198198
if (_size == 0)
199-
throw new InvalidOperationException(SR.InvalidOperation_EmptyStack);
199+
{
200+
ThrowForEmptyStack();
201+
}
202+
200203
return _array[_size - 1];
201204
}
202205

@@ -216,7 +219,10 @@ public bool TryPeek(out T result)
216219
public T Pop()
217220
{
218221
if (_size == 0)
219-
throw new InvalidOperationException(SR.InvalidOperation_EmptyStack);
222+
{
223+
ThrowForEmptyStack();
224+
}
225+
220226
_version++;
221227
T item = _array[--_size];
222228
_array[_size] = default(T); // Free memory quicker.
@@ -264,6 +270,12 @@ public T[] ToArray()
264270
return objArray;
265271
}
266272

273+
private void ThrowForEmptyStack()
274+
{
275+
Debug.Assert(_size == 0);
276+
throw new InvalidOperationException(SR.InvalidOperation_EmptyStack);
277+
}
278+
267279
[SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "not an expected scenario")]
268280
[Serializable]
269281
public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator

0 commit comments

Comments
 (0)