Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/System.Collections/src/System/Collections/Generic/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ IEnumerator IEnumerable.GetEnumerator()
public T Dequeue()
{
if (_size == 0)
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);
{
ThrowForEmptyQueue();
}

T removed = _array[_head];
_array[_head] = default(T);
Expand All @@ -244,8 +246,10 @@ public T Dequeue()
public T Peek()
{
if (_size == 0)
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);

{
ThrowForEmptyQueue();
}

return _array[_head];
}

Expand Down Expand Up @@ -328,6 +332,12 @@ private void MoveNext(ref int index)
index = (tmp == _array.Length) ? 0 : tmp;
}

private void ThrowForEmptyQueue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dequeue can call this as well, right? If nothing else it'd avoid duplicate usage of InvalidOperation_EmptyQueue. Not enough of a reason to separate it out into its own function, but as long as it is being separated out, might as well do so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I forgot Dequeue does this as well. Have updated.

{
Debug.Assert(_size == 0);
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);
}

public void TrimExcess()
{
int threshold = (int)(((double)_array.Length) * 0.9);
Expand Down