I am working with System.Array
and I found that the input parameter of ForEach
method public static void ForEach<T>(T[] array, Action<T> action);
is specified on one dimensional array case. I am trying to generalize this to multidimensional array.
The experimental implementation
Here's the experimental implementation of generalized ForEach
method.
class ArrayHelpers
{
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;
}
}
Test cases
The Test cases for 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 };
ArrayHelpers.ForEach<int>(test, ShowSquares);
// two dimensional case
Console.WriteLine("Two dimensional case");
int[,] test2 = { { 0, 1 }, { 2, 3 } };
ArrayHelpers.ForEach<int>(test2, ShowSquares);
// three dimensional case
Console.WriteLine("Three dimensional case");
int[,,] test3 = { { { 0, 1 }, { 2, 3 } }, { { 0, 1 }, { 2, 3 } } };
ArrayHelpers.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
I am wondering the things about the potential drawback or risks of this implementation.
All suggestions are welcome.