-
Notifications
You must be signed in to change notification settings - Fork 5k
[API Proposal]: ICollection<> AddRange / RemoveRange #98509
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
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsBackground and motivationIt is not quite uncommon to add several items at a time to a collection, which currently requires a loop with multiple calls to Many collection types including The addition of range api's to ICollection would not only make it more convenient to work with collections, it could also make common operations faster. API Proposalnamespace System.Collections.Generic;
public interface ICollection<T>
{
// Adds the elements of the specified collection the ICollection<T>.
// <remarks>The method is not atomic </remarks>
void AddRange(IEnumerable<T> collection)
{
ArgumentNullException.ThrowIfNull(collection);
foreach(item in collection)
Add(item);
}
// Removes the elements of the specified collection the ICollection<T>.
// <remarks>The method is not atomic </remarks>
void RemoveRange(IEnumerable<T> collection)
{
ArgumentNullException.ThrowIfNull(collection);
foreach(item in collection)
Remove(item);
}
} API UsageAssume we have extracted some keywords to attach to a blog entry. // blogPost .KeyWords has type ICollection<string>
BlogPost blogPost = ...;
List<string> keywords = ExtractTags(blogPost.Text); Then they can easily be added blogPost.Keywords.AddRange(keywords); Current alternatives: keywords.ForEach(x => blogPost.Keywords.Add(x));
// or
foreach(string keyword in keywords)
blogPost.Keywords.Add(keyword); Alternative Designs
Risks
|
This falls under the usual issues of retroactively introducing DIMs to existing interface types, which is known to have the potential to introduce both source breaking changes and run-time breaking changes. Given the prevalence of methods called In any case, the success of #95830 will most likely inform how we approach similar requests in the future. |
Uh oh!
There was an error while loading. Please reload this page.
Background and motivation
It is not quite uncommon to add several items at a time to a collection, which currently requires a loop with multiple calls to
.Add
if the exact type of collection is not known at compile type.Many collection types including
List<T>
,HashSet<T>
and hopefully soon? (#18087)Collection<T>
ObservableCollection<T>
provides optimized methods providing bulk Add and remove.By adding new methods to ICollection it becomes possible to easily write both succint and performant code
The addition of range api's to ICollection would not only make it more convenient to work with collections, it could also make common operations faster.
API Proposal
API Usage
Assume we have extracted some keywords to attach to a blog entry.
Then they can easily be added
Current alternatives:
Alternative Designs
ICollection<T>
interface is also quite popular for modelling relationships in EF CoreRisks
The text was updated successfully, but these errors were encountered: