Skip to content

Add Array.CreateFilled<T> overloads - #134812

Closed
SkyDevLab wants to merge 1 commit into
dotnet:mainfrom
SkyDevLab:feature/121477-array-createfilled
Closed

SkyDevLab wants to merge 1 commit into
dotnet:mainfrom
SkyDevLab:feature/121477-array-createfilled

Conversation

@SkyDevLab

@SkyDevLab SkyDevLab commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements the approved API enhancement from #121477:

namespace System;

public abstract partial class Array
{
    public static T[] CreateFilled<T>(int length, T value);
    public static T[] CreateFilled<T>(int length, Func<int, T> factory);
}

Design & Implementation Details

  • CreateFilled<T>(int length, T value):
    • Validates length >= 0 via ArgumentOutOfRangeException.ThrowIfNegative(length).
    • Returns cached empty array [] (Array.Empty<T>()) for length == 0.
    • Uses GC.AllocateUninitializedArray<T>(length) to bypass zeroing overhead for unmanaged/value types >= 2048 bytes while safely zero-initializing reference types.
    • Fills the array using new Span<T>(array).Fill(value), taking advantage of hardware/SIMD vectorization.
  • CreateFilled<T>(int length, Func<int, T> factory):
    • Validates length >= 0 via ArgumentOutOfRangeException.ThrowIfNegative(length).
    • Validates factory != null via ArgumentNullException.ThrowIfNull(factory).
    • Returns cached empty array [] for length == 0.
    • Allocates via GC.AllocateUninitializedArray<T>(length) and sequentially populates each element array[i] = factory(i).
    • Guarantees memory and GC safety even if factory throws midway during iteration.
  • Updated reference assembly contract in src/libraries/System.Runtime/ref/System.Runtime.cs.
  • Added comprehensive unit tests covering empty arrays, negative lengths, null factory validation, value types (small and >2048 bytes), reference types, structs with references, sequential indexing, reference instance distinctness, and exception propagation in src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs.

Fixes #121477

Implement approved API dotnet#121477: Array.CreateFilled<T>(int, T) and Array.CreateFilled<T>(int, Func<int, T>).

Fixes dotnet#121477
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 28, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@teo-tsirpanis

Copy link
Copy Markdown
Contributor

Duplicate of #134745.

@SkyDevLab

Copy link
Copy Markdown
Contributor Author

Duplicate of #134745.

Thanks for pointing that out. I’ll take a look at #134745 and see if any of the additional test coverage or implementation details from my PR can be useful there.

@SkyDevLab

Copy link
Copy Markdown
Contributor Author

Closing this PR in favor of #134745 since it was opened first. Looking forward to seeing the new Array.CreateFilled API landed in .NET!

@SkyDevLab SkyDevLab closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Array.Create<T> with default value and factory function

2 participants