Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic ForEach For Multidimensional Array #49112

Open
Jimmy-Hu opened this issue Mar 4, 2021 · 4 comments
Open

Generic ForEach For Multidimensional Array #49112

Jimmy-Hu opened this issue Mar 4, 2021 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Milestone

Comments

@Jimmy-Hu
Copy link

Jimmy-Hu commented Mar 4, 2021

Background and Motivation

There is a method Array.ForEach existed public static void ForEach<T>(T[] array, Action<T> action); and its input is specified on one dimensional array case. I want to propose another version Array.ForEach implementation for multidimensional array cases (ForEach([,], ...), ForEach([,,], ...) and ForEach([,,,], ...))

Proposed API

namespace System
{
    public abstract class Array : ICollection, IEnumerable, IList, IStructuralComparable, IStructuralEquatable, ICloneable
    {
+        public static void ForEach<T>(Array array, in Action<T> action)
+            where T : unmanaged
+        {
+            if (ReferenceEquals(array, null))
+            {
+                throw new ArgumentNullException(nameof(array));
+            }
+        
+            if (ReferenceEquals(action, null))
+            {
+                throw new ArgumentNullException(nameof(action));
+            }
+        
+            foreach (T item in array)
+            {
+                action.Invoke(item);
+            }
+            return;
+        }
    }
}

Usage Examples

The usage examples which include one dimensional case, two dimensional case and three dimensional case are as below.

//    One dimensional case
Console.WriteLine("One dimensional case");
int[] test = new int[] { 1, 2, 3, 4 };
Array.ForEach<int>(test, ShowSquares);

//    two dimensional case
Console.WriteLine("Two dimensional case");
int[,] test2 = { { 0, 1 }, { 2, 3 } };
Array.ForEach<int>(test2, ShowSquares);

//    three dimensional case
Console.WriteLine("Three dimensional case");
int[,,] test3 = { { { 0, 1 }, { 2, 3 } }, { { 0, 1 }, { 2, 3 } } };
Array.ForEach<int>(test3, ShowSquares);

/// <summary>
/// Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.foreach?view=net-5.0
/// </summary>
/// <param name="val">The input value for displaying and calculating square result</param>
private static void ShowSquares(int val)
{
    Console.WriteLine("{0:d} squared = {1:d}", val, val * val);
}

The output of the above tests:

One dimensional case
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
Two dimensional case
0 squared = 0
1 squared = 1
2 squared = 4
3 squared = 9
Three dimensional case
0 squared = 0
1 squared = 1
2 squared = 4
3 squared = 9
0 squared = 0
1 squared = 1
2 squared = 4
3 squared = 9

Reference

@Jimmy-Hu Jimmy-Hu added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 4, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 4, 2021
@Jimmy-Hu
Copy link
Author

Jimmy-Hu commented Mar 4, 2021

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

I think that the area is area-System.Runtime.

@huoyaoyuan
Copy link
Member

This implementation boxes and unboxes via non-generic IEnumerable. It should be not OK for new BCL api.

@huoyaoyuan
Copy link
Member

Related: #49269

If MD array implements generic IEnuemrable<T> in some way, this API will be more actionable.

@tannergooding tannergooding removed the untriaged New issue has not been triaged by the area owner label Jul 12, 2021
@tannergooding tannergooding added this to the Future milestone Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

4 participants