Class ObservableCollectionHelper
- Namespace
- VisioForge.Core.Helpers
- Assembly
- VisioForge.Core.dll
Provides extension methods for ObservableCollection<T> to perform bulk operations that would otherwise require multiple individual operations, potentially improving performance and reducing the number of collection change notifications.
public static class ObservableCollectionHelperInheritance
Inherited Members
Methods
AddRange<T>(ObservableCollection<T>, IEnumerable<T>)
Adds multiple items to the end of the ObservableCollection<T>.
public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> items)Parameters
collectionObservableCollection<T>-
The collection to add items to.
itemsIEnumerable<T>-
The enumerable collection of items to add.
Type Parameters
T-
The type of elements in the collection.
Remarks
This method adds each item individually, triggering a CollectionChanged event for each addition. For large collections, consider whether batch notifications would be more appropriate.
AddRange<T>(ObservableCollection<T>, T[])
Adds multiple items from an array to the end of the ObservableCollection<T>.
public static void AddRange<T>(this ObservableCollection<T> collection, T[] items)Parameters
collectionObservableCollection<T>-
The collection to add items to.
itemsT[]-
The array of items to add.
Type Parameters
T-
The type of elements in the collection.
Remarks
This overload is provided for convenience when working with arrays. Each item triggers a separate CollectionChanged event.
InsertRange<T>(ObservableCollection<T>, int, IEnumerable<T>)
Inserts multiple items into the ObservableCollection<T> at the specified index.
public static void InsertRange<T>(this ObservableCollection<T> collection, int index, IEnumerable<T> items)Parameters
collectionObservableCollection<T>-
The collection to insert items into.
indexint-
The zero-based index at which the first item should be inserted.
itemsIEnumerable<T>-
The enumerable collection of items to insert.
Type Parameters
T-
The type of elements in the collection.
Remarks
Items are inserted sequentially at consecutive indices starting from the specified index. Each insertion triggers a separate CollectionChanged event.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when index is negative or greater than the collection count.
RemoveRange<T>(ObservableCollection<T>, IEnumerable<T>)
Removes all occurrences of the specified items from the ObservableCollection<T>.
public static void RemoveRange<T>(this ObservableCollection<T> collection, IEnumerable<T> items)Parameters
collectionObservableCollection<T>-
The collection to remove items from.
itemsIEnumerable<T>-
The enumerable collection of items to remove.
Type Parameters
T-
The type of elements in the collection.
Remarks
Only the first occurrence of each item is removed. If an item appears multiple times in the collection, only the first instance will be removed. Each removal triggers a separate CollectionChanged event.
RemoveRange<T>(ObservableCollection<T>, int, int)
Removes a range of items from the ObservableCollection<T> starting at the specified index.
public static void RemoveRange<T>(this ObservableCollection<T> collection, int index, int count)Parameters
collectionObservableCollection<T>-
The collection to remove items from.
indexint-
The zero-based starting index of the range of items to remove.
countint-
The number of items to remove.
Type Parameters
T-
The type of elements in the collection.
Remarks
Items are removed one at a time from the same index position, as the collection shifts after each removal. Each removal triggers a separate CollectionChanged event.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when index is negative or greater than or equal to the collection count.
ToObservableCollection<T>(List<T>)
Converts a List<T> to an ObservableCollection<T>.
public static ObservableCollection<T> ToObservableCollection<T>(this List<T> collection)Parameters
collectionList<T>-
The list to convert.
Returns
- ObservableCollection<T>
-
A new ObservableCollection<T> containing all items from the source list.
Type Parameters
T-
The type of elements in the collection.
Remarks
This method creates a new ObservableCollection and copies all items from the source list. The original list remains unchanged.
ToObservableCollection<T>(T[])
Converts an array to an ObservableCollection<T>.
public static ObservableCollection<T> ToObservableCollection<T>(this T[] collection)Parameters
collectionT[]-
The array to convert.
Returns
- ObservableCollection<T>
-
A new ObservableCollection<T> containing all items from the source array.
Type Parameters
T-
The type of elements in the collection.
Remarks
This method creates a new ObservableCollection and copies all items from the source array. The original array remains unchanged.