[API Proposal]: Add IList<T> extension methods providing in-place update operations #76375
Open
Description
Background and motivation
The List<T>
type provides a number of methods providing bulk in-place updates, such as RemoveAll
, RemoveRange
, Reverse
and Sort
. The same cannot be said about IList<T>
where common in-place operations are cumbersome to achieve. Consider for instance the following System.Text.Json example used in our own docs repo:
A possible alternative to the above would be to roll your own extension method, but it would be even better if we shipped extension methods like that out of the box.
API Proposal
namespace System.Collections.Generic;
public partial static class CollectionExtensions
{
public static void AddRange<T>(this IList<T> list, IEnumerable<T> collection);
public static void InsertRange<T>(this IList<T> list, int index, IEnumerable<T> collection);
public static void RemoveAll<T>(this IList<T> list, Predicate<T> predicate);
public static void RemoveRange<T>(this IList<T> list, int index, int count);
public static void Reverse<T>(this IList<T> list);
public static void Sort<T>(this IList<T> list);
public static void Sort<T>(this IList<T> list, Comparison<T> comparison);
public static void Sort<T>(this IList<T> list, IComparer<T> comparer);
public static void Sort<T>(this IList<T> list, int index, int count, IComparer<T> comparer);
}
API Usage
Removing properties of a given type in System.Text.Json can now be expressed as follows:
public void ModifyTypeInfo(JsonTypeInfo ti)
{
if (ti.Kind != JsonTypeInfoKind.Object)
return;
ti.Properties.RemoveAll(prop => _ignoredTypes.Contains(prop.PropertyType));
}
}
Alternative Designs
- Should we consider exposing as DIMs?
- Should the
Sort
implementation be stable? Or matchList
/Array.Sort
semantics? - Should we include
BinarySearch
extension methods?
Risks
No response
Related to dotnet/core#2199
Activity