Skip to content

Proposal: Generic overload(s) of Array.Clear #18616

Description

@jamesqo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Runtime

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions