Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(fileContents, 0x1);
byte[] fileContents = Array.CreateFilled<byte>(contentSize, 0x1);

using MemoryStream archive = new MemoryStream();
using (GZipStream compressor = new GZipStream(archive, CompressionMode.Compress, leaveOpen: true))
Expand Down Expand Up @@ -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<byte>(actualData, (byte)'X');

byte[] actualData = Array.CreateFilled(dataSize, (byte)'X');

byte[] archive = BuildRawPaxArchiveWithSizeOverride("file.bin", "file.bin", actualData, dataSize, eaSize);

long apiLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(buffer, 0x1);
byte[] buffer = Array.CreateFilled<byte>(512, 0x1); // Minimum length of any header
malformed.Write(buffer);
malformed.Seek(0, SeekOrigin.Begin);

Expand Down Expand Up @@ -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<byte>(buffer, 0x0);
byte[] buffer = Array.CreateFilled<byte>(2048, 0x0); // Four additional end markers (512 each)
archive.Write(buffer);
archive.Seek(0, SeekOrigin.Begin);

Expand Down Expand Up @@ -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<byte>(fileContents, 0x1);
byte[] fileContents = Array.CreateFilled<byte>(contentSize, 0x1);

using MemoryStream archive = new MemoryStream();
{
Expand Down Expand Up @@ -544,9 +541,8 @@ public static IEnumerable<object[]> EASizeOverrideData()
[MemberData(nameof(EASizeOverrideData))]
public void PaxReader_EASizeOverridesHeaderSize(int actualDataSize, long headerSize, long eaSize)
{
byte[] actualData = new byte[actualDataSize];
Array.Fill<byte>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(MaxDistTreeElements, 5);
return staticDistanceTreeLength;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(buffer, 0xDE);

byte[] buffer = Array.CreateFilled<byte>(s_bufferSize + sentinelSize, 0xDE);

await Assert.ThrowsAsync<InvalidDataException>(async () =>
{
int read;
Expand Down
18 changes: 7 additions & 11 deletions src/libraries/System.Linq/tests/SumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,8 @@ public void SumOfInt_SourceSumsToOverflow_OverflowExceptionThrown()
[Fact]
public void SumOfInt_SourceSumsToOverflowVectorHorizontally_OverflowExceptionThrown()
{
int[] sourceInt = new int[Vector<int>.Count * 4];
Array.Fill(sourceInt, 0);

int[] sourceInt = Array.CreateFilled(Vector<int>.Count * 4, 0);

for (int i = 0; i < Vector<int>.Count; i++)
{
sourceInt[i] = int.MaxValue - 3;
Expand All @@ -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<int>.Count * 6];
Array.Fill(sourceInt, 0);
int[] sourceInt = Array.CreateFilled(Vector<int>.Count * 6, 0);

sourceInt[element] = int.MaxValue;
sourceInt[element + Vector<int>.Count * verticalOffset] = 1;
Expand All @@ -435,9 +433,8 @@ public void SumOfLong_SourceSumsToOverflow_OverflowExceptionThrown()
[Fact]
public void SumOfLong_SourceSumsToOverflowVectorHorizontally_OverflowExceptionThrown()
{
long[] sourceLong = new long[Vector<long>.Count * 4];
Array.Fill(sourceLong, 0);

long[] sourceLong = Array.CreateFilled<long>(Vector<long>.Count * 4, 0);

for (int i = 0; i < Vector<long>.Count; i++)
{
sourceLong[i] = long.MaxValue - 3;
Expand All @@ -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<long>.Count * 6];
Array.Fill(sourceLong, 0);

long[] sourceLong = Array.CreateFilled<long>(Vector<long>.Count * 6, 0);

sourceLong[element] = long.MaxValue;
sourceLong[element + Vector<long>.Count * verticalOffset] = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static void AsReadOnlyRef()
Assert.Equal(0x11222211, asInt);
Assert.True(Unsafe.AreSame<byte>(ref Unsafe.As<int, byte>(ref Unsafe.AsRef(in asInt)), ref MemoryMarshal.GetReference(span)));

var array = new byte[100];
Array.Fill<byte>(array, 0x42);
byte[] array = Array.CreateFilled<byte>(100, 0x42);
ref readonly TestHelpers.TestStructExplicit asStruct = ref MemoryMarshal.AsRef<TestHelpers.TestStructExplicit>(new ReadOnlySpan<byte>(array));

Assert.Equal((uint)0x42424242, asStruct.UI1);
Expand Down
3 changes: 1 addition & 2 deletions src/libraries/System.Memory/tests/MemoryMarshal/AsRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static void AsRef()
Assert.Equal(0x11222211, asInt);
Assert.True(Unsafe.AreSame<byte>(ref Unsafe.As<int, byte>(ref asInt), ref MemoryMarshal.GetReference(span)));

var array = new byte[100];
Array.Fill<byte>(array, 0x42);
byte[] array = Array.CreateFilled<byte>(100, 0x42);
ref TestHelpers.TestStructExplicit asStruct = ref MemoryMarshal.AsRef<TestHelpers.TestStructExplicit>(new Span<byte>(array));

Assert.Equal((uint)0x42424242, asStruct.UI1);
Expand Down
12 changes: 4 additions & 8 deletions src/libraries/System.Memory/tests/ReadOnlySpan/Count.byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ public static void TestAlignmentNotCount_RosByte()
[Fact]
public static void TestAlignmentCount_Byte()
{
byte[] array = new byte[4 * Vector<byte>.Count];
Array.Fill(array, (byte)5);
byte[] array = Array.CreateFilled<byte>(4 * Vector<byte>.Count, 5);
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count);
Expand All @@ -181,8 +180,7 @@ public static void TestAlignmentCount_Byte()
[Fact]
public static void TestAlignmentCount_RosByte()
{
byte[] array = new byte[4 * Vector<byte>.Count];
Array.Fill(array, (byte)5);
byte[] array = Array.CreateFilled<byte>(4 * Vector<byte>.Count, 5);
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count);
Expand Down Expand Up @@ -260,10 +258,8 @@ public static void MakeSureNoCountChecksGoOutOfRange_RosByte()
[Fact]
public static void TestOverlapDoNotCount_RosByte()
{
byte[] a = new byte[10];
Array.Fill<byte>(a, 6);


byte[] a = Array.CreateFilled<byte>(10, 6);

var span = new ReadOnlySpan<byte>(a);
Assert.Equal(5, span.Count(new byte[] { 6, 6 }));
}
Expand Down
13 changes: 5 additions & 8 deletions src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfAny.T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };

Expand Down Expand Up @@ -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++)
Expand Down Expand Up @@ -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++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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" };

Expand Down Expand Up @@ -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" };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(values1, values1));
Assert.Equal(600, TensorPrimitives.HammingBitDistance<T>(values1, values2));
Expand Down Expand Up @@ -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;

Expand All @@ -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<T>(x, min, one, destination);
Expand All @@ -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<T>(x, one, max, destination);
Expand All @@ -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;

Expand All @@ -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<T>(one, min, one, destination);
Expand All @@ -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<T>(one, one, max, destination);
Expand Down
13 changes: 5 additions & 8 deletions src/libraries/System.Numerics.Tensors/tests/TensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public static ReadOnlyCollection<T> AsReadOnly<T>(T[] array)
new ReadOnlyCollection<T>(array);
}

/// <summary>
/// Creates a new array of the specified length, with each element initialized to the specified value.
/// </summary>
/// <param name="length">The length of the array.</param>
/// <param name="value">The value with which to initialize each element.</param>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <returns>A new array of the specified length, with each element initialized to <paramref name="value"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is negative.</exception>
public static T[] CreateFilled<T>(int length, T value)
{
ArgumentOutOfRangeException.ThrowIfNegative(length);

T[] array = GC.AllocateUninitializedArray<T>(length);
Fill(array, value);
return array;
}

public static void Resize<T>([NotNull] ref T[]? array, int newSize)
{
if (newSize < 0)
Expand Down
Loading
Loading