Skip to content

Array.Create<T> with default value and factory function - #134745

Open
DanSamek wants to merge 8 commits into
dotnet:mainfrom
DanSamek:feature/121477
Open

DanSamek wants to merge 8 commits into
dotnet:mainfrom
DanSamek:feature/121477

Conversation

@DanSamek

Copy link
Copy Markdown

Fixes #121477

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 27, 2026
@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

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.


T[] array = GC.AllocateUninitializedArray<T>(length);

for (int i = 0; i < length; i++)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (int i = 0; i < length; i++)
for (int i = 0; i < array.Length; i++)

Pretty sure this is needed for the JIT to recognize the loop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit hard to find since it's in a different solution but the existing tests for Array are in src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So add the new tests to src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs where existing tests for Array are?

{
Assert.Throws<ArgumentOutOfRangeException>(() => System.Array.CreateFilled<int>(-1, index => index));
Assert.Throws<ArgumentOutOfRangeException>(() => System.Array.CreateFilled<object>(-1, index => new object()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good with tests for the non-factory method too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, i didn't find tests when i was implementing it :)

@jkotas

jkotas commented Sep 29, 2026

Copy link
Copy Markdown
Member

Are there any places in this repo that can be improved by switching to use these APIs?

@DanSamek

DanSamek commented Oct 3, 2026

Copy link
Copy Markdown
Author

@jkotas

jkotas commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Can you please update these places to use this API in this PR?

@DanSamek

DanSamek commented Oct 3, 2026

Copy link
Copy Markdown
Author

Can you please update these places to use this API in this PR?

Done, I replaced all occurrences matching that "pattern".

Future idea (out of scope for this PR): analyzer + code fix for this "pattern".

@@ -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, "");
var a = Array.CreateFilled(length, "");

@jkotas jkotas Oct 3, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var a = Array.CreateFilled(length, "");
string[] a = Array.CreateFilled(length, "");

The repo coding conventions allow var only when the type is explicitly named on the right-side.

https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/coding-style.md

(fix all instances)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo coding conventions allow var only when the type is explicitly named on the right-side.

https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/coding-style.md

(fix all instances)

My bad :)
It should be fixed.

/// <returns>A new array of the specified length, with each element initialized by <paramref name="factory"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="factory"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is negative.</exception>
public static T[] CreateFilled<T>(int length, Func<int, T> factory)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether it is wise to implement Func<int, T> overload at this point since there are no real-world examples so far where it would be beneficial to use it.

cc @agocke @tannergooding

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with it being deferred, particularly if we believe functional interfaces might come in the next release or two. I know Andy has a language proposal up for it now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did have usecases for it in the past personally, but I'd have more of them with it using functional interfaces.

This branch has not been deployed

No deployments
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

5 participants