SoFunction
Updated on 2025-04-15

C# efficiently implements collection batch addition and deletion operations

In C#, batch operations on a collection (such as batch adding or removing elements) often involve using methods and features provided by the collection type, as well as possible loops or LINQ queries to efficiently process large amounts of data. Here are some common methods and tips:

Add elements in batches

1. Use the AddRange method of the collection (if available)

Some collection types, such as List<T>, provide the AddRange method, allowing multiple elements to be added at once.

List<int> numbers = new List<int>();
int[] newNumbers = { 1, 2, 3, 4, 5 };
(newNumbers);

2. Use loops

For collection types that do not support AddRange, you can use a loop to add elements one by one, although this is not a true "batch" operation, it may be necessary in some cases.

HashSet<int> numbersSet = new HashSet<int>();
int[] newNumbers = { 1, 2, 3, 4, 5 };
foreach (var number in newNumbers)
{
    (number);
}

3. Use the Concat method of LINQ combined with ToList (or other collection constructors)

While this is not really a batch addition, you can use LINQ to combine collections and then create a new one.

List<int> originalList = new List<int> { 1, 2 };
int[] newElements = { 3, 4, 5 };
List<int> combinedList = (newElements).ToList();

Batch delete elements

1. Use the RemoveAll method of the collection (if available)

List<T> provides the RemoveAll method, allowing multiple elements to be deleted according to specified conditions.

List&lt;int&gt; numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5 };
(n =&gt; n &gt; 2); // Delete all elements larger than 2

2. Use the Except method combined with ToList (or other collection constructors)

For collection types that do not support RemoveAll, you can use LINQ's Except method to get the diff set and then create a new set (if needed).

List&lt;int&gt; originalList = new List&lt;int&gt; { 1, 2, 3, 4, 5 };
int[] elementsToRemove = { 3, 4, 5 };
List&lt;int&gt; filteredList = (elementsToRemove).ToList();
// Note: This does not modify the originalList, but creates a new list

3. Use loop and Remove methods

For simple scenarios, the Remove method of loops and collections can be used to delete elements one by one, but this method is less efficient, especially when the collections are larger.

HashSet<int> numbersSet = new HashSet<int> { 1, 2, 3, 4, 5 };
int[] elementsToRemove = { 3, 4, 5 };
foreach (var element in elementsToRemove)
{
    (element);
}

Performance considerations

Try to avoid modifying a collection in a loop: modifying it while traversing the collection (for example, removing elements in a foreach loop) may cause exceptions or undefined behavior. If you need to do this, consider using a temporary collection to store the elements you want to delete and then delete them outside the loop.

Select the correct collection type: Different collection types vary in performance characteristics. For example, List<T> is usually faster than LinkedList<T> when it comes to random access and adding/delete elements, while HashSet<T> is usually faster than Find and Delete operations.

Consider concurrency and thread safety: When operating a collection in a multi-threaded environment, make sure to use the appropriate synchronization mechanism to avoid race conditions and data corruption.

This is the article about C# efficiently implementing collection batch addition and deletion operations. For more related C# collection operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!