Fix more new T[0] to be Array.Empty<T>() (dotnet/corefx#27042)
authorStephen Toub <stoub@microsoft.com>
Sun, 11 Feb 2018 17:18:33 +0000 (12:18 -0500)
committerGitHub <noreply@github.com>
Sun, 11 Feb 2018 17:18:33 +0000 (12:18 -0500)
We've done several passes through the repo in the past.  More cases have snuck in since then.

Commit migrated from https://github.com/dotnet/corefx/commit/a127f01141ff4ffe2224da9ff63a1b5233ee5f06

29 files changed:
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLogInternal.cs
src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/Principal.cs
src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/BerConverter.cs
src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/DirectoryControl.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectorySearcher.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectorySynchronization.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectoryVirtualListViewContext.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ResultPropertyCollection.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/SchemaNameCollection.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Unix.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Unix.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Image.Windows.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Pen.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs
src/libraries/System.Net.HttpListener/src/System/Net/ServiceNameStore.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/XmlReaderDelegator.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/SchemaImporter.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/SoapReflectionImporter.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/Types.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSchemaImporter.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializerFactory.cs
src/libraries/System.Private.Xml/src/System/Xml/Xsl/XsltOld/DbgCompiler.cs
src/libraries/System.Private.Xml/src/System/Xml/Xsl/XsltOld/UseAttributeSetsAction.cs
src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs
src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs
src/libraries/System.Security.Permissions/src/System/Security/Permissions/PrincipalPermission.cs

index b8eb829..a82abbd 100644 (file)
@@ -656,7 +656,7 @@ namespace System.Diagnostics
                 throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName));
             }
 
-            string[] logNames = new string[0];
+            string[] logNames = null;
 
             RegistryKey eventkey = null;
             try
@@ -814,7 +814,7 @@ namespace System.Diagnostics
             }
 
             // If you pass in an empty array UnsafeTryFormatMessage will just pull out the message.
-            string formatString = UnsafeTryFormatMessage(hModule, messageNum, new string[0]);
+            string formatString = UnsafeTryFormatMessage(hModule, messageNum, Array.Empty<string>());
 
             if (formatString == null)
             {
index 0e466d2..f7f8501 100644 (file)
@@ -677,7 +677,7 @@ namespace System.Diagnostics
                 return null;
 
             if (insertionStrings == null)
-                insertionStrings = new string[0];
+                insertionStrings = Array.Empty<string>();
 
             string[] listDll = dllNameList.Split(';');
 
@@ -1398,7 +1398,7 @@ namespace System.Diagnostics
         {
             // check arguments
             if (strings == null)
-                strings = new string[0];
+                strings = Array.Empty<string>();
             if (strings.Length >= 256)
                 throw new ArgumentException(SR.TooManyReplacementStrings);
 
@@ -1413,7 +1413,7 @@ namespace System.Diagnostics
                     throw new ArgumentException(SR.LogEntryTooLong);
             }
             if (rawData == null)
-                rawData = new byte[0];
+                rawData = Array.Empty<byte>();
 
             if (Source.Length == 0)
                 throw new ArgumentException(SR.NeedSourceToWrite);
index 20b17a6..3b7ff4a 100644 (file)
@@ -635,7 +635,7 @@ namespace System.DirectoryServices.AccountManagement
             }
             else if (this.unpersisted)
             {
-                return new object[0];
+                return Array.Empty<object>();
             }
             else
             {
@@ -645,7 +645,7 @@ namespace System.DirectoryServices.AccountManagement
                 int valCount = de.Properties[attribute].Count;
 
                 if (valCount == 0)
-                    return new object[0];
+                    return Array.Empty<object>();
                 else
                 {
                     object[] objectArray = new object[valCount];
index 42a2ca8..354170e 100644 (file)
@@ -22,7 +22,7 @@ namespace System.DirectoryServices.Protocols
             byte[] encodingResult = null;
             // value is allowed to be null in certain scenario, so if it is null, just set it to empty array.
             if (value == null)
-                value = new object[0];
+                value = Array.Empty<object>();
 
             Debug.WriteLine("Begin encoding\n");
 
@@ -235,7 +235,7 @@ namespace System.DirectoryServices.Protocols
 
                 if (binaryValue == null || binaryValue.bv_len == 0)
                 {
-                    encodingResult = new byte[0];
+                    encodingResult = Array.Empty<byte>();
                 }
                 else
                 {
index 679a970..0ef7370 100644 (file)
@@ -634,7 +634,7 @@ namespace System.DirectoryServices.Protocols
 
     public class SortRequestControl : DirectoryControl
     {
-        private SortKey[] _keys = new SortKey[0];
+        private SortKey[] _keys = Array.Empty<SortKey>();
         public SortRequestControl(params SortKey[] sortKeys) : base("1.2.840.113556.1.4.473", null, true, true)
         {
             if (sortKeys == null)
index 8895609..6f94c2f 100644 (file)
@@ -671,7 +671,7 @@ namespace System.DirectoryServices
             else
             {
                 adsSearch.ExecuteSearch(Filter, null, -1, out resultsHandle);
-                properties = new string[0];
+                properties = Array.Empty<string>();
             }
 
             SearchResultCollection result = new SearchResultCollection(clonedRoot, resultsHandle, properties, this);
index 7f0a090..d8adada 100644 (file)
@@ -9,7 +9,7 @@ namespace System.DirectoryServices
     public class DirectorySynchronization
     {
         private DirectorySynchronizationOptions _option = DirectorySynchronizationOptions.None;
-        private byte[] _cookie = new byte[0];
+        private byte[] _cookie = Array.Empty<byte>();
 
         public DirectorySynchronization()
         {
index b3c2393..a25af4d 100644 (file)
@@ -8,7 +8,7 @@ namespace System.DirectoryServices
     {
         internal readonly byte[] _context;
 
-        public DirectoryVirtualListViewContext() : this(new byte[0])
+        public DirectoryVirtualListViewContext() : this(null)
         {
         }
 
@@ -16,7 +16,7 @@ namespace System.DirectoryServices
         {
             if (context == null)
             {
-                _context = new byte[0];
+                _context = Array.Empty<byte>();
             }
             else
             {
index 7e93bb1..7ed4132 100644 (file)
@@ -30,7 +30,7 @@ namespace System.DirectoryServices
                 }
                 else
                 {
-                    return new ResultPropertyValueCollection(new object[0]);
+                    return new ResultPropertyValueCollection(Array.Empty<object>());
                 }
             }
         }
index 4b1d26e..7097d57 100644 (file)
@@ -103,8 +103,7 @@ namespace System.DirectoryServices
         /// </devdoc>
         public void Clear()
         {
-            object[] newValues = new object[0];
-            _propSetter(newValues);
+            _propSetter(Array.Empty<object>());
         }
 
         /// <devdoc>
@@ -128,7 +127,7 @@ namespace System.DirectoryServices
         {
             object value = _propGetter();
             if (value == null)
-                return new object[0];
+                return Array.Empty<object>();
             else
                 return (object[])value;
         }
index 1392545..c7d8c1c 100644 (file)
@@ -1777,7 +1777,7 @@ namespace System.Drawing
         public Region[] MeasureCharacterRanges(string text, Font font, RectangleF layoutRect, StringFormat stringFormat)
         {
             if ((text == null) || (text.Length == 0))
-                return new Region[0];
+                return Array.Empty<Region>();
 
             if (font == null)
                 throw new ArgumentNullException("font");
@@ -1787,7 +1787,7 @@ namespace System.Drawing
 
             int regcount = stringFormat.GetMeasurableCharacterRangeCount();
             if (regcount == 0)
-                return new Region[0];
+                return Array.Empty<Region>();
 
             IntPtr[] native_regions = new IntPtr[regcount];
             Region[] regions = new Region[regcount];
index 119dcae..f6dc345 100644 (file)
@@ -466,7 +466,7 @@ namespace System.Drawing
 
             IconImage ii = new IconImage();
             ii.iconHeader = bih;
-            ii.iconColors = new uint[0];    // no palette
+            ii.iconColors = Array.Empty<uint>();    // no palette
             int xor_size = (((bih.biBitCount * bitmap.Width + 31) & ~31) >> 3) * bitmap.Height;
             ii.iconXOR = new byte[xor_size];
             int p = 0;
index c0b8f1a..1e4ed87 100644 (file)
@@ -866,7 +866,7 @@ namespace System.Drawing
                 Debug.Assert(count >= 0, "FrameDimensionsList returns bad count");
                 if (count <= 0)
                 {
-                    return new Guid[0];
+                    return Array.Empty<Guid>();
                 }
 
                 int size = (int)Marshal.SizeOf(typeof(Guid));
@@ -1059,7 +1059,7 @@ namespace System.Drawing
                     throw SafeNativeMethods.Gdip.StatusException(status);
 
                 if (size == 0 || count == 0)
-                    return new PropertyItem[0];
+                    return Array.Empty<PropertyItem>();
 
                 IntPtr propdata = Marshal.AllocHGlobal(size);
                 try
index 16cc007..f044480 100644 (file)
@@ -748,7 +748,7 @@ namespace System.Drawing
                 }
                 else if (DashStyle == DashStyle.Solid)
                 {
-                    pattern = new float[0];
+                    pattern = Array.Empty<float>();
                 }
                 else
                 {
index ccc03be..8e1084b 100644 (file)
@@ -1079,7 +1079,7 @@ namespace System.Drawing.Printing
 
             int count = FastDeviceCapabilities(SafeNativeMethods.DC_PAPERNAMES, IntPtr.Zero, -1, printerName);
             if (count == -1)
-                return new PaperSize[0];
+                return Array.Empty<PaperSize>();
             int stringSize = Marshal.SystemDefaultCharSize * 64;
             IntPtr namesBuffer = Marshal.AllocCoTaskMem(checked(stringSize * count));
             FastDeviceCapabilities(SafeNativeMethods.DC_PAPERNAMES, namesBuffer, -1, printerName);
@@ -1123,7 +1123,7 @@ namespace System.Drawing.Printing
 
             int count = FastDeviceCapabilities(SafeNativeMethods.DC_BINNAMES, IntPtr.Zero, -1, printerName);
             if (count == -1)
-                return new PaperSource[0];
+                return Array.Empty<PaperSource>();
 
             // Contrary to documentation, DeviceCapabilities returns char[count, 24],
             // not char[count][24]
index fedc94b..0459834 100644 (file)
@@ -297,11 +297,11 @@ namespace System.Net
                 }
                 catch (System.Net.Sockets.SocketException)
                 {
-                    return new string[0];
+                    return Array.Empty<string>();
                 }
                 catch (System.Security.SecurityException)
                 {
-                    return new string[0];
+                    return Array.Empty<string>();
                 }
             }
             else if (!hostname.Contains("."))
index e6f4764..8bc75ef 100644 (file)
@@ -123,7 +123,7 @@ namespace System.Net.NetworkInformation
             // If we don't have any interfaces detected, return empty.
             if (result == Interop.IpHlpApi.ERROR_NO_DATA || result == Interop.IpHlpApi.ERROR_INVALID_PARAMETER)
             {
-                return new SystemNetworkInterface[0];
+                return Array.Empty<SystemNetworkInterface>();
             }
 
             // Otherwise we throw on an error.
index 6acfc8c..dc18ceb 100644 (file)
@@ -502,7 +502,7 @@ namespace System.Runtime.Serialization
         public virtual byte[] ReadContentAsBase64()
         {
             if (isEndOfEmptyElement)
-                return new byte[0];
+                return Array.Empty<byte>();
 
             if (dictionaryReader == null)
             {
index 974aa82..8bf4b20 100644 (file)
@@ -155,7 +155,7 @@ namespace System.Xml.Serialization
             TypeDesc typeDesc = Scope.GetTypeDesc(typeof(object));
             StructMapping mapping = new StructMapping();
             mapping.TypeDesc = typeDesc;
-            mapping.Members = new MemberMapping[0];
+            mapping.Members = Array.Empty<MemberMapping>();
             mapping.IncludeInSchema = false;
             mapping.TypeName = Soap.UrType;
             mapping.Namespace = XmlSchema.Namespace;
index 05838f6..383946e 100644 (file)
@@ -266,7 +266,7 @@ namespace System.Xml.Serialization
             StructMapping mapping = new StructMapping();
             mapping.IsSoap = true;
             mapping.TypeDesc = typeDesc;
-            mapping.Members = new MemberMapping[0];
+            mapping.Members = Array.Empty<MemberMapping>();
             mapping.IncludeInSchema = false;
             mapping.TypeName = Soap.UrType;
             mapping.Namespace = XmlSchema.Namespace;
@@ -739,7 +739,7 @@ namespace System.Xml.Serialization
                 attribute.Mapping = ImportTypeMapping(_modelScope.GetTypeModel(accessorType), (a.SoapAttribute == null ? String.Empty : a.SoapAttribute.DataType), limiter);
                 attribute.Default = GetDefaultValue(model.FieldTypeDesc, a);
                 accessor.Attribute = attribute;
-                accessor.Elements = new ElementAccessor[0];
+                accessor.Elements = Array.Empty<ElementAccessor>();
             }
             else
             {
index 079ec5e..8f099cc 100644 (file)
@@ -1253,7 +1253,7 @@ namespace System.Xml.Serialization
         {
             if (typeof(IEnumerable).IsAssignableFrom(type))
             {
-                MethodInfo enumerator = type.GetMethod("GetEnumerator", new Type[0]);
+                MethodInfo enumerator = type.GetMethod("GetEnumerator", Array.Empty<Type>());
 
                 if (enumerator == null || !typeof(IEnumerator).IsAssignableFrom(enumerator.ReturnType))
                 {
index 8dd5777..6d4dc7c 100644 (file)
@@ -1166,7 +1166,7 @@ namespace System.Xml.Serialization
             accessor.Mapping = mapping;
 
             MemberMapping member = new MemberMapping();
-            member.Elements = new ElementAccessor[0];
+            member.Elements = Array.Empty<ElementAccessor>();
             member.Text = accessor;
             if (isMixed)
             {
@@ -1499,7 +1499,7 @@ namespace System.Xml.Serialization
             AttributeAccessor accessor = ImportAttribute(attribute, identifier, ns, attribute);
             if (accessor == null) return;
             MemberMapping member = new MemberMapping();
-            member.Elements = new ElementAccessor[0];
+            member.Elements = Array.Empty<ElementAccessor>();
             member.Attribute = accessor;
             member.Name = CodeIdentifier.MakeValid(Accessor.UnescapeName(accessor.Name));
             member.Name = membersScope.AddUnique(member.Name, member);
@@ -1529,7 +1529,7 @@ namespace System.Xml.Serialization
             accessor.Mapping = mapping;
 
             MemberMapping member = new MemberMapping();
-            member.Elements = new ElementAccessor[0];
+            member.Elements = Array.Empty<ElementAccessor>();
             member.Attribute = accessor;
             member.Name = membersScope.MakeRightCase("AnyAttr");
             member.Name = membersScope.AddUnique(member.Name, member);
@@ -1584,7 +1584,7 @@ namespace System.Xml.Serialization
 
             xmlnsMapping.TypeDesc = xmlnsTypeDesc;
             xmlnsMapping.TypeName = xmlnsMapping.TypeDesc.Name;
-            xmlnsMapping.Members = new MemberMapping[0];
+            xmlnsMapping.Members = Array.Empty<MemberMapping>();
             xmlnsMapping.IncludeInSchema = false;
             xmlnsMapping.ReferencedByTopLevelElement = true;
 
index c9882b4..28fc31d 100644 (file)
@@ -1106,7 +1106,7 @@ namespace System.Xml.Serialization
             if (arraySize != null && arraySize.Length > 0)
                 dimensions = arraySize.Split(null);
             else
-                dimensions = new string[0];
+                dimensions = Array.Empty<string>();
 
             soapArrayInfo.dimensions = 0;
             soapArrayInfo.length = -1;
@@ -2411,7 +2411,7 @@ namespace System.Xml.Serialization
 
         internal void GenerateEnd()
         {
-            GenerateEnd(new string[0], new XmlMapping[0], new Type[0]);
+            GenerateEnd(Array.Empty<string>(), Array.Empty<XmlMapping>(), Array.Empty<Type>());
         }
         internal void GenerateEnd(string[] methods, XmlMapping[] xmlMappings, Type[] types)
         {
@@ -3573,7 +3573,7 @@ namespace System.Xml.Serialization
                 Writer.Write(", ");
                 WriteQuotedCSharpString(structMapping.Namespace);
                 Writer.WriteLine(");");
-                members = new Member[0];
+                members = Array.Empty<Member>();
                 anyFixups = false;
                 fixupMethodName = null;
             }
index cfd9374..95f2f17 100644 (file)
@@ -40,7 +40,7 @@ namespace System.Xml.Serialization
         /// </devdoc>
         public XmlSerializer CreateSerializer(Type type, XmlRootAttribute root)
         {
-            return CreateSerializer(type, null, new Type[0], root, null, null);
+            return CreateSerializer(type, null, Array.Empty<Type>(), root, null, null);
         }
 
         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer3"]/*' />
@@ -58,7 +58,7 @@ namespace System.Xml.Serialization
         /// </devdoc>
         public XmlSerializer CreateSerializer(Type type, XmlAttributeOverrides overrides)
         {
-            return CreateSerializer(type, overrides, new Type[0], null, null, null);
+            return CreateSerializer(type, overrides, Array.Empty<Type>(), null, null, null);
         }
 
         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer5"]/*' />
index 0dce03c..aa992b9 100644 (file)
@@ -32,7 +32,7 @@ namespace System.Xml.Xsl.XsltOld
         private DbgData()
         {
             _styleSheet = null;
-            _variables = new VariableAction[0];
+            _variables = Array.Empty<VariableAction>();
         }
         public static DbgData Empty { get { return s_nullDbgData; } }
     }
index bac0d33..7f1fc34 100644 (file)
@@ -32,7 +32,7 @@ namespace System.Xml.Xsl.XsltOld
             if (_useString.Length == 0)
             {
                 // Split creates empty node is spliting empty string
-                _useAttributeSets = new XmlQualifiedName[0];
+                _useAttributeSets = Array.Empty<XmlQualifiedName>();
                 return;
             }
 
@@ -56,7 +56,7 @@ namespace System.Xml.Xsl.XsltOld
                     throw;
                 }
                 // Ignore the whole list in forwards-compatible mode
-                _useAttributeSets = new XmlQualifiedName[0];
+                _useAttributeSets = Array.Empty<XmlQualifiedName>();
             }
         }
 
index c043fcd..afe1bb1 100644 (file)
@@ -118,7 +118,7 @@ namespace System.Security.Cryptography.Xml
         internal byte[] GetDigestedBytes(HashAlgorithm hash)
         {
             _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr);
-            hash.TransformFinalBlock(new byte[0], 0, 0);
+            hash.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
             byte[] res = (byte[])hash.Hash.Clone();
             // reinitialize the hash so it is still usable after the call
             hash.Initialize();
index 2b1bc1d..bd91b30 100644 (file)
@@ -66,7 +66,7 @@ namespace System.Security.Cryptography.Xml
         internal byte[] GetDigestedBytes(HashAlgorithm hash)
         {
             _c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr);
-            hash.TransformFinalBlock(new byte[0], 0, 0);
+            hash.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
             byte[] res = (byte[])hash.Hash.Clone();
             // reinitialize the hash so it is still usable after the call
             hash.Initialize();
index f7f3feb..0655e32 100644 (file)
@@ -291,7 +291,7 @@ namespace System.Security.Permissions
                 }
             }
             else
-                _idArray = new IDRole[0];
+                _idArray = Array.Empty<IDRole>();
         }
 
         public override string ToString()