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

Commit bfe5c6a

Browse files
committed
Merge pull request #2344 from James-Ko/patch-3
Replace all non-test uses of new T[0] with Array.Empty<T>
2 parents 6b6ac8d + 9b1ed39 commit bfe5c6a

File tree

13 files changed

+16
-22
lines changed

13 files changed

+16
-22
lines changed

src/Common/src/System/Dynamic/Utils/EmptyReadOnlyCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ namespace System.Dynamic.Utils
1010
{
1111
internal static class EmptyReadOnlyCollection<T>
1212
{
13-
public static ReadOnlyCollection<T> Instance = new TrueReadOnlyCollection<T>(new T[0]);
13+
public static ReadOnlyCollection<T> Instance = new TrueReadOnlyCollection<T>(Array.Empty<T>());
1414
}
1515
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ public T[] ToArray()
309309
{
310310
T[] arr = new T[_size];
311311
if (_size == 0)
312-
return arr;
313-
312+
return arr; // consider replacing with Array.Empty<T>() to be consistent with non-generic Queue
313+
314314
if (_head < _tail)
315315
{
316316
Array.Copy(_array, _head, arr, 0, _size);

src/System.Dynamic.Runtime/src/System/Dynamic/DynamicMetaObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class DynamicMetaObject
2222
/// Represents an empty array of type <see cref="DynamicMetaObject"/>. This field is read only.
2323
/// </summary>
2424
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2105:ArrayFieldsShouldNotBeReadOnly")]
25-
public static readonly DynamicMetaObject[] EmptyMetaObjects = new DynamicMetaObject[0];
25+
public static readonly DynamicMetaObject[] EmptyMetaObjects = Array.Empty<DynamicMetaObject>();
2626

2727
/// <summary>
2828
/// Initializes a new instance of the <see cref="DynamicMetaObject"/> class.

src/System.Dynamic.Runtime/src/System/Runtime/CompilerServices/RuleCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace System.Runtime.CompilerServices
1515
[EditorBrowsable(EditorBrowsableState.Never), DebuggerStepThrough]
1616
public class RuleCache<T> where T : class
1717
{
18-
private T[] _rules = new T[0];
18+
private T[] _rules = Array.Empty<T>();
1919
private readonly Object _cacheLock = new Object();
2020

2121
private const int MaxRules = 128;

src/System.Linq/src/System/Linq/Enumerable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3159,7 +3159,7 @@ internal Buffer(IEnumerable<TElement> source, bool queryInterfaces = true)
31593159

31603160
internal TElement[] ToArray()
31613161
{
3162-
if (count == 0) return new TElement[0];
3162+
if (count == 0) return new TElement[0]; // consider replacing with Array.Empty<TElement>()
31633163
if (items.Length == count) return items;
31643164

31653165
var arr = new TElement[count];

src/System.Security.Cryptography.Encryption.Aes/src/Internal/Cryptography/AesCngCryptoDecryptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected sealed override byte[] UncheckedTransformFinalBlock(SafeKeyHandle hKey
126126
}
127127
else
128128
{
129-
outputData = new byte[0];
129+
outputData = Array.Empty<byte>();
130130
}
131131
return outputData;
132132
}

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/ChainPal.GetChainStatusInformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal sealed partial class ChainPal : IDisposable, IChainPal
2525
private static X509ChainStatus[] GetChainStatusInformation(CertTrustErrorStatus dwStatus)
2626
{
2727
if (dwStatus == CertTrustErrorStatus.CERT_TRUST_NO_ERROR)
28-
return new X509ChainStatus[0];
28+
return Array.Empty<X509ChainStatus>();
2929

3030
int count = 0;
3131
for (uint bits = (uint)dwStatus; bits != 0; bits = bits >> 1)

src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509Chain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public X509ChainStatus[] ChainStatus
5151
// We give the user a reference to the array since we'll never access it.
5252
X509ChainStatus[] chainStatus = _lazyChainStatus;
5353
if (chainStatus == null)
54-
chainStatus = _lazyChainStatus = (_pal == null ? new X509ChainStatus[0] : _pal.ChainStatus);
54+
chainStatus = _lazyChainStatus = (_pal == null ? Array.Empty<X509ChainStatus>() : _pal.ChainStatus);
5555
return chainStatus;
5656
}
5757
}

src/System.Xml.XDocument/src/System/Xml/Linq/XAttribute.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ namespace System.Xml.Linq
1818
[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "Reviewed.")]
1919
public class XAttribute : XObject
2020
{
21-
private static IEnumerable<XAttribute> s_emptySequence;
22-
2321
/// <summary>
2422
/// Gets an empty collection of attributes.
2523
/// </summary>
2624
public static IEnumerable<XAttribute> EmptySequence
2725
{
2826
get
2927
{
30-
if (s_emptySequence == null) s_emptySequence = new XAttribute[0];
31-
return s_emptySequence;
28+
return Array.Empty<XAttribute>();
3229
}
3330
}
3431

src/System.Xml.XDocument/src/System/Xml/Linq/XElement.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@ namespace System.Xml.Linq
3030
[XmlSchemaProvider(null, IsAny = true)]
3131
public class XElement : XContainer, IXmlSerializable
3232
{
33-
private static IEnumerable<XElement> s_emptySequence;
34-
3533
/// <summary>
3634
/// Gets an empty collection of elements.
3735
/// </summary>
3836
public static IEnumerable<XElement> EmptySequence
3937
{
4038
get
4139
{
42-
if (s_emptySequence == null) s_emptySequence = new XElement[0];
43-
return s_emptySequence;
40+
return Array.Empty<XElement>();
4441
}
4542
}
4643

0 commit comments

Comments
 (0)