Separated from #18497
Background
It may be beneficial to provide generic Array.Clear overloads that accept T[] rather than an Array. Since we know the type of these arrays at compile-time and that they're 0-based, we can write the clearing logic inline in C#. This may benefit smaller arrays since we don't suffer the overhead of calling into the runtime, and will eliminate the need for workarounds like this. (For large arrays, of course, we can just continue what we were doing before.)
It may also be desirable to provide an overload that does not accept an index/length, e.g. Clear(T[]), which clears the entire array. Implementing this will be cheaper than had we done it with Array, since we can call Clear(array, 0, array.Length) instead of Clear(array, array.GetLowerBound(0), array.GetLength(0)).
Proposed API
namespace System
{
public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
{
public static void Clear<T>(T[] array);
public static void Clear<T>(T[] array, int index, int length);
}
}
@terrajobst
Separated from #18497
Background
It may be beneficial to provide generic
Array.Clearoverloads that acceptT[]rather than anArray. Since we know the type of these arrays at compile-time and that they're 0-based, we can write the clearing logic inline in C#. This may benefit smaller arrays since we don't suffer the overhead of calling into the runtime, and will eliminate the need for workarounds like this. (For large arrays, of course, we can just continue what we were doing before.)It may also be desirable to provide an overload that does not accept an index/length, e.g.
Clear(T[]), which clears the entire array. Implementing this will be cheaper than had we done it withArray, since we can callClear(array, 0, array.Length)instead ofClear(array, array.GetLowerBound(0), array.GetLength(0)).Proposed API
@terrajobst