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

Commit 6337e28

Browse files
committed
Remove unnecessary empty array static fields
As I was touching Stack, I noticed that it and Queue had an empty array static field, which was just being initialized to Array.Empty(). The field is unnecessary, and the call site can just use Array.Empty directly. This commit just cleans that up and some unnecessary field setting in the ctors.
1 parent cd15d09 commit 6337e28

File tree

2 files changed

+2
-15
lines changed

2 files changed

+2
-15
lines changed

src/System.Collections/src/System/Collections/Generic/Queue.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ public class Queue<T> : IEnumerable<T>,
3232
private const int MinimumGrow = 4;
3333
private const int GrowFactor = 200; // double each time
3434
private const int DefaultCapacity = 4;
35-
private static T[] s_emptyArray = Array.Empty<T>();
3635

3736
// Creates a queue with room for capacity objects. The default initial
3837
// capacity and grow factor are used.
3938
/// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue"]/*' />
4039
public Queue()
4140
{
42-
_array = s_emptyArray;
41+
_array = Array.Empty<T>();
4342
}
4443

4544
// Creates a queue with room for capacity objects. The default grow factor
@@ -50,11 +49,7 @@ public Queue(int capacity)
5049
{
5150
if (capacity < 0)
5251
throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_NeedNonNegNumRequired);
53-
5452
_array = new T[capacity];
55-
_head = 0;
56-
_tail = 0;
57-
_size = 0;
5853
}
5954

6055
// Fills a Queue with the elements of an ICollection. Uses the enumerator
@@ -67,8 +62,6 @@ public Queue(IEnumerable<T> collection)
6762
throw new ArgumentNullException("collection");
6863

6964
_array = new T[DefaultCapacity];
70-
_size = 0;
71-
_version = 0;
7265

7366
using (IEnumerator<T> en = collection.GetEnumerator())
7467
{

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ public class Stack<T> : IEnumerable<T>,
2929
private Object _syncRoot;
3030

3131
private const int DefaultCapacity = 4;
32-
private static T[] s_emptyArray = Array.Empty<T>();
3332

3433
/// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Stack"]/*' />
3534
public Stack()
3635
{
37-
_array = s_emptyArray;
38-
_size = 0;
39-
_version = 0;
36+
_array = Array.Empty<T>();
4037
}
4138

4239
// Create a stack with a specific initial capacity. The initial capacity
@@ -47,8 +44,6 @@ public Stack(int capacity)
4744
if (capacity < 0)
4845
throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_NeedNonNegNumRequired);
4946
_array = new T[capacity];
50-
_size = 0;
51-
_version = 0;
5247
}
5348

5449
// Fills a Stack with the contents of a particular collection. The items are
@@ -59,7 +54,6 @@ public Stack(IEnumerable<T> collection)
5954
{
6055
if (collection == null)
6156
throw new ArgumentNullException("collection");
62-
6357
_array = EnumerableHelpers.ToArray(collection, out _size);
6458
}
6559

0 commit comments

Comments
 (0)