diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs index ba3768bf425908..d584b5d05fb1ec 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs @@ -265,8 +265,7 @@ private async Task Extract_LinkEntry_TargetInsideDirectory_Internal(bool async, [MemberData(nameof(GetContentSizeAndBooleanData))] public async Task Extract_UnseekableStream_BlockAlignmentPadding_DoesNotAffectNextEntries(int contentSize, bool async) { - byte[] fileContents = new byte[contentSize]; - Array.Fill(fileContents, 0x1); + byte[] fileContents = Array.CreateFilled(contentSize, 0x1); using MemoryStream archive = new MemoryStream(); using (GZipStream compressor = new GZipStream(archive, CompressionMode.Compress, leaveOpen: true)) @@ -612,9 +611,8 @@ public void PaxExtraction_PathTraversalInEA_IsBlocked() [InlineData(100, 25)] // EA smaller than header public void PaxExtraction_EntryLengthMatchesExtractedFileSize(int dataSize, long eaSize) { - byte[] actualData = new byte[dataSize]; - Array.Fill(actualData, (byte)'X'); - + byte[] actualData = Array.CreateFilled(dataSize, (byte)'X'); + byte[] archive = BuildRawPaxArchiveWithSizeOverride("file.bin", "file.bin", actualData, dataSize, eaSize); long apiLength; diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs index 7985f48a5a8f7c..656854acce7330 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs @@ -44,8 +44,7 @@ public async Task MalformedArchive_TooSmall(bool async) public async Task MalformedArchive_HeaderSize(bool async) { using MemoryStream malformed = new MemoryStream(); - byte[] buffer = new byte[512]; // Minimum length of any header - Array.Fill(buffer, 0x1); + byte[] buffer = Array.CreateFilled(512, 0x1); // Minimum length of any header malformed.Write(buffer); malformed.Seek(0, SeekOrigin.Begin); @@ -92,8 +91,7 @@ public async Task LongEndMarkers_DoNotAdvanceStream(bool async) await WriteEntry(writer, entry, async); } - byte[] buffer = new byte[2048]; // Four additional end markers (512 each) - Array.Fill(buffer, 0x0); + byte[] buffer = Array.CreateFilled(2048, 0x0); // Four additional end markers (512 each) archive.Write(buffer); archive.Seek(0, SeekOrigin.Begin); @@ -337,8 +335,7 @@ public async Task GetNextEntry_UnseekableArchive_ReplaceDataStream_ExcludeFromDi [InlineData(1023, true, true)] public async Task BlockAlignmentPadding_DoesNotAffectNextEntries(int contentSize, bool copyData, bool async) { - byte[] fileContents = new byte[contentSize]; - Array.Fill(fileContents, 0x1); + byte[] fileContents = Array.CreateFilled(contentSize, 0x1); using MemoryStream archive = new MemoryStream(); { @@ -544,9 +541,8 @@ public static IEnumerable EASizeOverrideData() [MemberData(nameof(EASizeOverrideData))] public void PaxReader_EASizeOverridesHeaderSize(int actualDataSize, long headerSize, long eaSize) { - byte[] actualData = new byte[actualDataSize]; - Array.Fill(actualData, (byte)'X'); - + byte[] actualData = Array.CreateFilled(actualDataSize, (byte)'X'); + byte[] archive = BuildRawPaxArchiveWithSizeOverride("file.bin", "file.bin", actualData, headerSize, eaSize); using var stream = new MemoryStream(archive); diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/HuffmanTree.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/HuffmanTree.cs index 10b903b6918c4b..97e5d04e2c3c0b 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/HuffmanTree.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/HuffmanTree.cs @@ -89,8 +89,7 @@ private static byte[] GetStaticLiteralTreeLength() private static byte[] GetStaticDistanceTreeLength() { - byte[] staticDistanceTreeLength = new byte[MaxDistTreeElements]; - Array.Fill(staticDistanceTreeLength, (byte)5); + byte[] staticDistanceTreeLength = Array.CreateFilled(MaxDistTreeElements, 5); return staticDistanceTreeLength; } diff --git a/src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs b/src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs index cd455fb6a78bb7..1e428baee02c28 100644 --- a/src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs +++ b/src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs @@ -327,9 +327,8 @@ public static async Task ZipArchiveEntry_CorruptedStream_EnsureNoExtraBytesReadO // the window portion [0, s_bufferSize), so the tail [s_bufferSize, end) must // never be touched regardless of how many iterations the loop takes. const int sentinelSize = 64; - byte[] buffer = new byte[s_bufferSize + sentinelSize]; - Array.Fill(buffer, 0xDE); - + byte[] buffer = Array.CreateFilled(s_bufferSize + sentinelSize, 0xDE); + await Assert.ThrowsAsync(async () => { int read; diff --git a/src/libraries/System.Linq/tests/SumTests.cs b/src/libraries/System.Linq/tests/SumTests.cs index b94a6873dcd24b..f30643e2bd6930 100644 --- a/src/libraries/System.Linq/tests/SumTests.cs +++ b/src/libraries/System.Linq/tests/SumTests.cs @@ -388,9 +388,8 @@ public void SumOfInt_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfInt_SourceSumsToOverflowVectorHorizontally_OverflowExceptionThrown() { - int[] sourceInt = new int[Vector.Count * 4]; - Array.Fill(sourceInt, 0); - + int[] sourceInt = Array.CreateFilled(Vector.Count * 4, 0); + for (int i = 0; i < Vector.Count; i++) { sourceInt[i] = int.MaxValue - 3; @@ -407,8 +406,7 @@ public void SumOfInt_SourceSumsToOverflowVectorHorizontally_OverflowExceptionThr [MemberData(nameof(SumOverflowsVerticalVectorLanes))] public void SumOfInt_SourceSumsToOverflowVectorVertically_OverflowExceptionThrown(int element, int verticalOffset) { - int[] sourceInt = new int[Vector.Count * 6]; - Array.Fill(sourceInt, 0); + int[] sourceInt = Array.CreateFilled(Vector.Count * 6, 0); sourceInt[element] = int.MaxValue; sourceInt[element + Vector.Count * verticalOffset] = 1; @@ -435,9 +433,8 @@ public void SumOfLong_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfLong_SourceSumsToOverflowVectorHorizontally_OverflowExceptionThrown() { - long[] sourceLong = new long[Vector.Count * 4]; - Array.Fill(sourceLong, 0); - + long[] sourceLong = Array.CreateFilled(Vector.Count * 4, 0); + for (int i = 0; i < Vector.Count; i++) { sourceLong[i] = long.MaxValue - 3; @@ -454,9 +451,8 @@ public void SumOfLong_SourceSumsToOverflowVectorHorizontally_OverflowExceptionTh [MemberData(nameof(SumOverflowsVerticalVectorLanes))] public void SumOfLong_SourceSumsToOverflowVectorVertically_OverflowExceptionThrown(int element, int verticalOffset) { - long[] sourceLong = new long[Vector.Count * 6]; - Array.Fill(sourceLong, 0); - + long[] sourceLong = Array.CreateFilled(Vector.Count * 6, 0); + sourceLong[element] = long.MaxValue; sourceLong[element + Vector.Count * verticalOffset] = 1; diff --git a/src/libraries/System.Memory/tests/MemoryMarshal/AsReadOnlyRef.cs b/src/libraries/System.Memory/tests/MemoryMarshal/AsReadOnlyRef.cs index e48790231b2888..9bd4afaa6b1eb9 100644 --- a/src/libraries/System.Memory/tests/MemoryMarshal/AsReadOnlyRef.cs +++ b/src/libraries/System.Memory/tests/MemoryMarshal/AsReadOnlyRef.cs @@ -18,8 +18,7 @@ public static void AsReadOnlyRef() Assert.Equal(0x11222211, asInt); Assert.True(Unsafe.AreSame(ref Unsafe.As(ref Unsafe.AsRef(in asInt)), ref MemoryMarshal.GetReference(span))); - var array = new byte[100]; - Array.Fill(array, 0x42); + byte[] array = Array.CreateFilled(100, 0x42); ref readonly TestHelpers.TestStructExplicit asStruct = ref MemoryMarshal.AsRef(new ReadOnlySpan(array)); Assert.Equal((uint)0x42424242, asStruct.UI1); diff --git a/src/libraries/System.Memory/tests/MemoryMarshal/AsRef.cs b/src/libraries/System.Memory/tests/MemoryMarshal/AsRef.cs index c84606fb966b6f..53de83bd912100 100644 --- a/src/libraries/System.Memory/tests/MemoryMarshal/AsRef.cs +++ b/src/libraries/System.Memory/tests/MemoryMarshal/AsRef.cs @@ -18,8 +18,7 @@ public static void AsRef() Assert.Equal(0x11222211, asInt); Assert.True(Unsafe.AreSame(ref Unsafe.As(ref asInt), ref MemoryMarshal.GetReference(span))); - var array = new byte[100]; - Array.Fill(array, 0x42); + byte[] array = Array.CreateFilled(100, 0x42); ref TestHelpers.TestStructExplicit asStruct = ref MemoryMarshal.AsRef(new Span(array)); Assert.Equal((uint)0x42424242, asStruct.UI1); diff --git a/src/libraries/System.Memory/tests/ReadOnlySpan/Count.byte.cs b/src/libraries/System.Memory/tests/ReadOnlySpan/Count.byte.cs index 642a24c567e919..f3080966bf3647 100644 --- a/src/libraries/System.Memory/tests/ReadOnlySpan/Count.byte.cs +++ b/src/libraries/System.Memory/tests/ReadOnlySpan/Count.byte.cs @@ -166,8 +166,7 @@ public static void TestAlignmentNotCount_RosByte() [Fact] public static void TestAlignmentCount_Byte() { - byte[] array = new byte[4 * Vector.Count]; - Array.Fill(array, (byte)5); + byte[] array = Array.CreateFilled(4 * Vector.Count, 5); for (var i = 0; i < Vector.Count; i++) { var span = new ReadOnlySpan(array, i, 3 * Vector.Count); @@ -181,8 +180,7 @@ public static void TestAlignmentCount_Byte() [Fact] public static void TestAlignmentCount_RosByte() { - byte[] array = new byte[4 * Vector.Count]; - Array.Fill(array, (byte)5); + byte[] array = Array.CreateFilled(4 * Vector.Count, 5); for (var i = 0; i < Vector.Count; i++) { var span = new ReadOnlySpan(array, i, 3 * Vector.Count); @@ -260,10 +258,8 @@ public static void MakeSureNoCountChecksGoOutOfRange_RosByte() [Fact] public static void TestOverlapDoNotCount_RosByte() { - byte[] a = new byte[10]; - Array.Fill(a, 6); - - + byte[] a = Array.CreateFilled(10, 6); + var span = new ReadOnlySpan(a); Assert.Equal(5, span.Count(new byte[] { 6, 6 })); } diff --git a/src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfAny.T.cs b/src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfAny.T.cs index da57b026907644..fee8a1415049b1 100644 --- a/src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfAny.T.cs +++ b/src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfAny.T.cs @@ -495,8 +495,7 @@ public static void DefaultFilledIndexOfAny_TwoString() for (int length = 0; length < byte.MaxValue; length++) { - var a = new string[length]; - Array.Fill(a, ""); + string[] a = Array.CreateFilled(length, ""); string[] targets = { "", "99" }; @@ -634,9 +633,8 @@ public static void DefaultFilledIndexOfAny_ThreeString() for (int length = 0; length < byte.MaxValue; length++) { - var a = new string[length]; - Array.Fill(a, ""); - + string[] a = Array.CreateFilled(length, ""); + string[] targets = { "", "99", "98" }; for (int i = 0; i < length; i++) @@ -782,9 +780,8 @@ public static void DefaultFilledIndexOfAny_ManyString() { for (int length = 0; length < byte.MaxValue; length++) { - var a = new string[length]; - Array.Fill(a, ""); - + string[] a = Array.CreateFilled(length, ""); + var values = new string[] { "", "99", "98", "0" }; for (int i = 0; i < length; i++) diff --git a/src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOfAny.T.cs b/src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOfAny.T.cs index 0c81bc33840cc0..8cef1271dc6907 100644 --- a/src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOfAny.T.cs +++ b/src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOfAny.T.cs @@ -488,9 +488,8 @@ public static void DefaultFilledLastIndexOfAny_String_TwoByte() for (int length = 0; length < byte.MaxValue; length++) { - var a = new string[length]; - Array.Fill(a, ""); - + string[] a = Array.CreateFilled(length, ""); + string[] targets = { "", "99" }; for (int i = 0; i < length; i++) @@ -624,8 +623,7 @@ public static void DefaultFilledLastIndexOfAny_String_ThreeByte() for (int length = 0; length < byte.MaxValue; length++) { - var a = new string[length]; - Array.Fill(a, ""); + string[] a = Array.CreateFilled(length, ""); string[] targets = { "", "99", "98" }; @@ -769,8 +767,7 @@ public static void DefaultFilledLastIndexOfAny_String_ManyByte() { for (int length = 0; length < byte.MaxValue; length++) { - var a = new string[length]; - Array.Fill(a, ""); + string[] a = Array.CreateFilled(length, ""); var values = new string[] { "", "99", "98", "0" }; diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/NegotiateAuthenticationKerberosTest.ChannelBindings.cs b/src/libraries/System.Net.Security/tests/FunctionalTests/NegotiateAuthenticationKerberosTest.ChannelBindings.cs index 6c59f38cc739f4..5293f99e87bbb7 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/NegotiateAuthenticationKerberosTest.ChannelBindings.cs +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/NegotiateAuthenticationKerberosTest.ChannelBindings.cs @@ -103,8 +103,7 @@ private static SafeChannelBindingHandle CreateChannelBinding(ChannelBindingKind { const int HashLength = 32; SafeChannelBindingHandle handle = new SafeChannelBindingHandle(kind); - byte[] hash = new byte[HashLength]; - Array.Fill(hash, hashSeed); + byte[] hash = Array.CreateFilled(HashLength, hashSeed); handle.SetCertHash(hash); return handle; } diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs index ee7ba743833e05..7e52e539e70b67 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs @@ -2708,11 +2708,8 @@ public void HammingBitDistance_KnownValues() T value42 = T.CreateTruncating(42); T value84 = T.CreateTruncating(84); - T[] values1 = new T[100]; - T[] values2 = new T[100]; - - Array.Fill(values1, value42); - Array.Fill(values2, value84); + T[] values1 = Array.CreateFilled(100, value42); + T[] values2 = Array.CreateFilled(100, value84); Assert.Equal(0, TensorPrimitives.HammingBitDistance(values1, values1)); Assert.Equal(600, TensorPrimitives.HammingBitDistance(values1, values2)); @@ -2885,10 +2882,9 @@ public void Clamp_UnorderedBoundsMatchScalarSemantics(int length) { // Span x, span min, span max { - T[] min = new T[length]; - T[] max = new T[length]; - Array.Fill(min, zero); - Array.Fill(max, two); + T[] min = Array.CreateFilled(length, zero); + T[] max = Array.CreateFilled(length, two); + min[offendingIndex] = two; max[offendingIndex] = zero; @@ -2902,8 +2898,7 @@ public void Clamp_UnorderedBoundsMatchScalarSemantics(int length) // Span x, span min, scalar max { - T[] min = new T[length]; - Array.Fill(min, zero); + T[] min = Array.CreateFilled(length, zero); min[offendingIndex] = two; TensorPrimitives.Clamp(x, min, one, destination); @@ -2916,8 +2911,7 @@ public void Clamp_UnorderedBoundsMatchScalarSemantics(int length) // Span x, scalar min, span max { - T[] max = new T[length]; - Array.Fill(max, two); + T[] max = Array.CreateFilled(length, two); max[offendingIndex] = zero; TensorPrimitives.Clamp(x, one, max, destination); @@ -2940,10 +2934,8 @@ public void Clamp_UnorderedBoundsMatchScalarSemantics(int length) // Scalar x, span min, span max { - T[] min = new T[length]; - T[] max = new T[length]; - Array.Fill(min, zero); - Array.Fill(max, two); + T[] min = Array.CreateFilled(length, zero); + T[] max = Array.CreateFilled(length, two); min[offendingIndex] = two; max[offendingIndex] = zero; @@ -2957,8 +2949,7 @@ public void Clamp_UnorderedBoundsMatchScalarSemantics(int length) // Scalar x, span min, scalar max { - T[] min = new T[length]; - Array.Fill(min, zero); + T[] min = Array.CreateFilled(length, zero); min[offendingIndex] = two; TensorPrimitives.Clamp(one, min, one, destination); @@ -2971,8 +2962,7 @@ public void Clamp_UnorderedBoundsMatchScalarSemantics(int length) // Scalar x, scalar min, span max { - T[] max = new T[length]; - Array.Fill(max, two); + T[] max = Array.CreateFilled(length, two); max[offendingIndex] = zero; TensorPrimitives.Clamp(one, one, max, destination); diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs b/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs index e559170d29046a..09c64df6263bd9 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs @@ -1127,9 +1127,8 @@ public static void TensorIndexOfMinNonDenseTests(int[] data, nint[] shape, nint[ { _ = expectedLogical; // Set all logical elements to positive values, then put the minimum at the last logical position - int[] testData = new int[data.Length]; - Array.Fill(testData, 50); - + int[] testData = Array.CreateFilled(data.Length, 50); + nint flatLen = 1; foreach (nint s in shape) { @@ -1173,9 +1172,8 @@ public static void TensorIndexOfMinNonDenseTests(int[] data, nint[] shape, nint[ public static void TensorIndexOfMaxMagnitudeNonDenseTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical) { _ = expectedLogical; - int[] testData = new int[data.Length]; - Array.Fill(testData, 1); - + int[] testData = Array.CreateFilled(data.Length, 1); + nint flatLen = 1; foreach (nint s in shape) { @@ -1219,8 +1217,7 @@ public static void TensorIndexOfMaxMagnitudeNonDenseTests(int[] data, nint[] sha public static void TensorIndexOfMinMagnitudeNonDenseTests(int[] data, nint[] shape, nint[] strides, int[] expectedLogical) { _ = expectedLogical; - int[] testData = new int[data.Length]; - Array.Fill(testData, 100); + int[] testData = Array.CreateFilled(data.Length, 100); nint flatLen = 1; foreach (nint s in shape) diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.cs b/src/libraries/System.Private.CoreLib/src/System/Array.cs index fc2c8f8248311d..cc5825b20b4765 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.cs @@ -39,6 +39,23 @@ public static ReadOnlyCollection AsReadOnly(T[] array) new ReadOnlyCollection(array); } + /// + /// Creates a new array of the specified length, with each element initialized to the specified value. + /// + /// The length of the array. + /// The value with which to initialize each element. + /// The type of the elements in the array. + /// A new array of the specified length, with each element initialized to . + /// is negative. + public static T[] CreateFilled(int length, T value) + { + ArgumentOutOfRangeException.ThrowIfNegative(length); + + T[] array = GC.AllocateUninitializedArray(length); + Fill(array, value); + return array; + } + public static void Resize([NotNull] ref T[]? array, int newSize) { if (newSize < 0) diff --git a/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/Helpers/AhoCorasickNode.cs b/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/Helpers/AhoCorasickNode.cs index d2f2d9de6f07cf..b0b8c281e6d6f6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/Helpers/AhoCorasickNode.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/Helpers/AhoCorasickNode.cs @@ -155,8 +155,7 @@ static bool TryCreateJumpTable(Dictionary children, [NotNullWhen(true return false; } - table = new int[maxValue + 1]; - Array.Fill(table, -1); + table = Array.CreateFilled(maxValue + 1, -1); foreach ((char childChar, int childIndex) in children) { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextWriter.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextWriter.cs index d45397bb634f11..34bd2a87473e9a 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextWriter.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextWriter.cs @@ -143,8 +143,7 @@ private enum Token private static char[] CreateDefaultIndentChars() { - var result = new char[IndentArrayLength]; - Array.Fill(result, DefaultIndentChar); + char[] result = Array.CreateFilled(IndentArrayLength, DefaultIndentChar); return result; } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 2489f8cd6df48a..d92ae1dd819941 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -528,6 +528,7 @@ public static void Copy(System.Array sourceArray, int sourceIndex, System.Array public static void Copy(System.Array sourceArray, long sourceIndex, System.Array destinationArray, long destinationIndex, long length) { } public void CopyTo(System.Array array, int index) { } public void CopyTo(System.Array array, long index) { } + public static T[] CreateFilled(int length, T value) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The code for an array of the specified type might not be available.")] public static System.Array CreateInstance(System.Type elementType, int length) { throw null; } public static System.Array CreateInstance(System.Type elementType, int length1, int length2) { throw null; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs index eaa93ba957d048..5251e103319ad5 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs @@ -4631,6 +4631,51 @@ public static void Array_Initialize() Assert.All(array2, Assert.Null); } + [Theory] + [InlineData(0, 10)] + [InlineData(5, 10)] + [InlineData(1000, 10)] + public void CreateFilled_Value_ValueType(int length, int value) + { + int[] array = Array.CreateFilled(length, value); + var expected = new int[length]; + for (int i = 0; i < expected.Length; i++) + expected[i] = value; + + Assert.Equal(length, array.Length); + Assert.Equal(expected, array); + } + + [Theory] + [InlineData(0)] + [InlineData(5)] + [InlineData(1000)] + public void CreateFilled_Value_ReferenceType(int length) + { + var value = new object(); + object[] array = Array.CreateFilled(length, value); + var expected = new object[length]; + for (int i = 0; i < expected.Length; i++) + expected[i] = value; + + Assert.Equal(length, array.Length); + Assert.Equal(expected, array); + } + + [Fact] + public void CreateFilled_Value_Empty() + { + int[] array = Array.CreateFilled(0, 0); + Assert.Empty(array); + } + + [Fact] + public void CreateFilled_NegativeLength_Throws() + { + Assert.Throws("length", () => Array.CreateFilled(-1, new object())); + Assert.Throws("length", () => Array.CreateFilled(-1, 8)); + } + private static void VerifyArray(Array array, Type elementType, int[] lengths, int[] lowerBounds, object repeatedValue) { VerifyArray(array, elementType, lengths, lowerBounds); @@ -4923,6 +4968,7 @@ public class ClassWithNonGenericInterfaceWithNonGenericInterface1 : NonGenericIn public abstract class AbstractClass { } public static class StaticClass { } + private record Foo2(int Value); public enum SByteEnum : sbyte { diff --git a/src/tests/JIT/opt/Cloning/OffsetLimit.cs b/src/tests/JIT/opt/Cloning/OffsetLimit.cs index 25a1752053dab0..1ad954fe2b82bc 100644 --- a/src/tests/JIT/opt/Cloning/OffsetLimit.cs +++ b/src/tests/JIT/opt/Cloning/OffsetLimit.cs @@ -248,8 +248,7 @@ public static void VarMinusKTest(int n, int len) [InlineData(ushort.MaxValue, 66535)] public static void UShortVarPlusKTest(ushort src, int expected) { - int[] a = new int[70000]; - Array.Fill(a, 1); + int[] a = Array.CreateFilled(70000, 1); Assert.Equal(expected, UShortVarPlusK(a, src)); Assert.Throws(() => UShortVarPlusK(new int[expected - 1], src)); } diff --git a/src/tests/JIT/opt/SVE/PredicateInstructions.cs b/src/tests/JIT/opt/SVE/PredicateInstructions.cs index c223c9026fb587..12e9b15e94654a 100644 --- a/src/tests/JIT/opt/SVE/PredicateInstructions.cs +++ b/src/tests/JIT/opt/SVE/PredicateInstructions.cs @@ -17,12 +17,10 @@ public class PredicateInstructions [ConditionalFact(typeof(Sve), nameof(Sve.IsSupported))] public static void TestMaskElementGranularity() { - int[] integers = new int[Vector.Count]; - Array.Fill(integers, 1); + int[] integers = Array.CreateFilled(Vector.Count, 1); integers[0] = 0; - short[] shorts = new short[Vector.Count]; - Array.Fill(shorts, (short)1); + short[] shorts = Array.CreateFilled(Vector.Count, 1); shorts[0] = 0; Vector right = new Vector(integers);