Provides a generic list that is able to notify its consumer about changes and supports data binding.
See the Remarks section for the differences compared to the BindingListT class.
See the Remarks section for the differences compared to the BindingListT class.
Inheritance Hierarchy
KGySoft.Collections.ObjectModelVirtualCollectionT
KGySoft.Collections.ObjectModelFastLookupCollectionT
KGySoft.ComponentModelFastBindingListT
KGySoft.ComponentModelSortableBindingListT
Namespace: KGySoft.ComponentModel
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 7.0.0-preview.3
Syntax
[SerializableAttribute] public class FastBindingList<T> : FastLookupCollection<T>, IBindingList, ICollection, IEnumerable, IList, ICancelAddNew, IRaiseItemChangedEvents, IDisposable
Type Parameters
- T
- The type of elements in the list.
The FastBindingListT type exposes the following members.
Constructors
Name | Description | |
---|---|---|
![]() | FastBindingListT |
Initializes a new instance of the FastBindingListT class with a CircularListT internally.
|
![]() | FastBindingListT(IListT) |
Initializes a new instance of the FastBindingListT class with the specified list.
|
Properties
Name | Description | |
---|---|---|
![]() | AllowEdit |
Gets or sets whether item properties can be edited in the list.
Default value: . |
![]() | AllowNew |
Gets or sets whether new items can be added to the list by the AddNew method.
Default value: if the wrapped list is not read-only and T is a value type or has a parameterless constructor; otherwise, . |
![]() | AllowRemove | |
![]() | CanSetItem |
Gets whether an item can be set through the indexer.
(Inherited from VirtualCollectionT.)The base implementation returns if IsReadOnly returns or when the wrapped collection is a one dimensional zero based array of T; otherwise, returns . |
![]() | CheckConsistency |
Gets or sets whether consistency of the stored items should be checked when items are get or set in the collection.
(Inherited from FastLookupCollectionT.)Default value: , if the FastLookupCollectionT was initialized by the default constructor; otherwise, as it was specified. |
![]() | Count |
Gets the number of elements actually contained in the VirtualCollectionT.
(Inherited from VirtualCollectionT.)The base implementation returns the Count property of the underlying collection. |
![]() | IsReadOnly |
Gets whether the VirtualCollectionT is read-only. Affects the behavior of Add, Insert,
Remove, RemoveAt and Clear methods.
(Inherited from VirtualCollectionT.)The base implementation returns the IsReadOnly property of the underlying collection. |
![]() | IsSorted |
Gets whether the items in the list are sorted.
|
![]() | IsSortedCore |
Gets whether the list is sorted.
The base implementation returns . |
![]() | Item |
Gets or sets the element at the specified index.
(Inherited from VirtualCollectionT.)When read, calls the overridable GetItem method, and when set, calls the overridable SetItem method. |
![]() | Items |
Gets the wrapped underlying collection maintained by this VirtualCollectionT instance.
(Inherited from VirtualCollectionT.) |
![]() | PropertyDescriptors |
Gets the property descriptors of T.
|
![]() | RaiseListChangedEvents |
Gets or sets whether adding or removing items within the list raises ListChanged events.
Default value: . |
![]() | SortDirectionCore |
Gets the direction of the sort.
The base implementation returns Ascending. |
![]() | SortProperty |
Gets a PropertyDescriptor that is being used for sorting. Returns if the list is not sorted or
when it is sorted by the values of T rather than by one of its properties.
|
![]() | SortPropertyCore |
Gets the property descriptor that is used for sorting the list if sorting, or if the list is not sorted or
when it is sorted by the values of T rather than by one of its properties.
The base implementation returns . |
![]() | SupportsChangeNotificationCore | |
![]() | SupportsSearchingCore |
Gets whether the list supports searching.
The base implementation returns . |
![]() | SupportsSortingCore |
Gets whether the list supports sorting.
The base implementation returns . |
Methods
Name | Description | |
---|---|---|
![]() | Add | (Inherited from VirtualCollectionT.) |
![]() | AddIndexCore |
If overridden in a derived class, adds the PropertyDescriptors to the indices used for searching.
The base implementation does nothing. |
![]() | AddNew |
Adds a new item to the collection.
|
![]() | AddNewCore |
Adds a new item to the collection.
|
![]() | ApplySort(ListSortDirection) |
Sorts the list by the values of T rather than one of its properties based on the specified direction.
|
![]() | ApplySort(PropertyDescriptor, ListSortDirection) |
Sorts the list based on the specified property and direction.
|
![]() | ApplySort(String, ListSortDirection) |
Sorts the list based on the specified propertyName and direction.
|
![]() | ApplySortCore |
If overridden in a derived class, sorts the items of the list.
The base implementation throws a NotSupportedException. |
![]() | CancelNew |
Discards a pending new item added by the AddNew method.
|
![]() | Clear | (Inherited from VirtualCollectionT.) |
![]() | ClearItems |
Removes all elements from the FastBindingListT.
(Overrides FastLookupCollectionTClearItems.) |
![]() | Contains |
Determines whether an element is in the VirtualCollectionT.
(Inherited from VirtualCollectionT.)Calls the overridable ContainsItem method. |
![]() | ContainsItem |
Gets whether the specified item is in the VirtualCollectionT.
(Inherited from VirtualCollectionT.)The base implementation calls the GetItemIndex method. |
![]() | CopyTo |
Copies the entire VirtualCollectionT to a compatible one-dimensional Array, starting at the specified arrayIndex of the target array.
(Inherited from VirtualCollectionT.)Calls the overridable GetItem method for each index between zero and Count, excluding upper bound. |
![]() | Dispose |
Releases the list and removes both incoming and outgoing subscriptions.
|
![]() | Dispose(Boolean) |
Releases the resources used by this FastBindingListT instance.
|
![]() | EndNew |
Commits a pending new item of any position added by the AddNew method.
|
![]() | EndNew(Int32) |
Commits a pending new item added by the AddNew method.
|
![]() | Find(PropertyDescriptor, Object) |
Searches for the index of the item that has the specified property descriptor with the specified value.
|
![]() | Find(String, Object) |
Searches for the index of the item that has the specified property descriptor with the specified value.
|
![]() | FindCore |
Searches for the index of the item that has the specified property descriptor with the specified value.
The base implementation performs a linear search on the items. |
![]() | GetEnumerator |
Returns an enumerator that iterates through the VirtualCollectionT.
(Inherited from VirtualCollectionT.)The base implementation returns the enumerator of the underlying collection. |
![]() | GetItem |
Gets the element at the specified index.
(Overrides VirtualCollectionTGetItem(Int32).) |
![]() | GetItemIndex |
Gets the zero-based index of the first of the specified item within the FastLookupCollectionT.
(Inherited from FastLookupCollectionT.) |
![]() | IndexOf |
Searches for the specified object and returns the zero-based index of an occurrence within the entire VirtualCollectionT.
(Inherited from VirtualCollectionT.)Calls the overridable GetItemIndex method. |
![]() | InnerListChanged |
Invalidates the internally stored index mapping. Call if the wrapped list that has been passed to the constructor has been changed explicitly.
(Inherited from FastLookupCollectionT.) |
![]() | Insert |
Inserts an element into the VirtualCollectionT at the specified index.
(Inherited from VirtualCollectionT.)Calls the overridable InsertItem method. |
![]() | InsertItem |
Inserts an element into the FastBindingListT at the specified index.
(Overrides FastLookupCollectionTInsertItem(Int32, T).) |
![]() | OnAddingNew |
Raises the AddingNew event.
|
![]() | OnListChanged |
Raises the ListChanged event.
|
![]() | OnMapRebuilt |
Called after the internal index map has been rebuilt either when inconsistency has been detected or when InnerListChanged has been called.
(Overrides FastLookupCollectionTOnMapRebuilt.) |
![]() | Remove |
Removes one occurrence of a specific object from the VirtualCollectionT.
(Inherited from VirtualCollectionT.)Calls the overridable RemoveItem method. |
![]() | RemoveAt |
Removes the element at the specified index of the VirtualCollectionT.
(Inherited from VirtualCollectionT.)Calls the overridable RemoveItem method. |
![]() | RemoveIndexCore |
If overridden in a derived class, removes the PropertyDescriptors from the indices used for searching.
The base implementation does nothing. |
![]() | RemoveItem |
Removes the first occurrence of item from the FastLookupCollectionT.
(Inherited from FastLookupCollectionT.) |
![]() | RemoveItemAt |
Removes the element at the specified index from the FastBindingListT.
(Overrides FastLookupCollectionTRemoveItemAt(Int32).) |
![]() | RemoveSort |
Removes any sort applied by the ApplySort overloads.
|
![]() | RemoveSortCore |
Removes any sort applied by the ApplySort overloads.
The base implementation throws a NotSupportedException. |
![]() | ResetBindings |
Raises the ListChanged event of type Reset.
|
![]() | ResetItem | |
![]() | SetItem |
Replaces the item at the specified index.
(Overrides FastLookupCollectionTSetItem(Int32, T).) |
Events
Name | Description | |
---|---|---|
![]() | AddingNew |
Occurs when a new item is added to the list by the AddNew method.
|
![]() | ListChanged |
Occurs when the list or an item in the list changes.
|
Extension Methods
Name | Description | |
---|---|---|
![]() | Convert(Type, CultureInfo) | Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example. |
![]() ![]() | ConvertTTarget(CultureInfo) | Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Remarks section for details. |
![]() | In |
Gets whether item is among the elements of set.
(Defined by ObjectExtensions.)See the Examples section of the generic InT(T, T) overload for an example. |
![]() | IndexOf(FuncObject, Boolean) | Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.) |
![]() | IndexOf(Object) | Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.) |
![]() | IsNullOrEmpty |
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions.) |
![]() | TryAdd |
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddRange |
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryClear |
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryConvert(Type, Object) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryConvert(Type, CultureInfo, Object) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryConvertTTarget(TTarget) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryConvertTTarget(CultureInfo, TTarget) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryGetCount |
Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions.) |
![]() | TryGetElementAt |
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsert |
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertRange |
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemove |
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveAt |
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveRange |
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.) |
![]() | TryReplaceRange |
Tries to remove count amount of items from the target at the specified index, and
to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.) |
![]() | TrySetElementAt |
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
Remarks
![]() |
---|
FastBindingListT is mainly compatible with BindingListT but has a better performance than that because element lookup in FastBindingListT is an O(1) operation. In contrast, element lookup in BindingListT is an O(n) operation, which makes BindingList<T>.AddNew and BindingList<T>.ListChanged invocation (when an element is changed) slow because they call the Collection{T}.IndexOf method to determine the position of the added or changed element. |
Comparison with BindingListT
Incompatibility with BindingListT:
- BindingListT is derived from CollectionT, whereas FastBindingListT is derived from FastLookupCollectionT, which is derived from VirtualCollectionT. Both types implement the IListT interface though.
- BindingList<T>.AddingNew event has AddingNewEventHandler type, which uses AddingNewEventArgs, whereas in FastBindingListT the AddingNew event has EventHandlerTEventArgs type where TEventArgs is AddingNewEventArgsT. The main difference between the two event arguments that the latter is generic.
- In FastBindingListT the AllowRemove property is initialized to if the wrapped list is read-only.
In contrast, in BindingListT this property is by default. - In FastBindingListT the AllowNew property is initialized to if the wrapped list is read-only, or when T is not a value type and has no parameterless constructor.
The return value of AllowNew does not change when AddingNew event is subscribed and setting AllowNew does not reset the list.
In contrast, in BindingListT this property is if T is not a primitive type and has no public parameterless constructor. However, return value of BindingList<T>.AllowNew can change when BindingList<T>.AddingNew event is subscribed, and setting the BindingList<T>.AllowNew property resets the list. - Calling AddNew throws InvalidOperationException if AllowNew is .
- Calling Remove or Clear throws InvalidOperationException if AllowRemove is .
- AddNewCore returns T instead of object.
- If AddNewCore is called for a T type, which cannot be instantiated automatically and the AddingNew event is not subscribed or returns , then an InvalidOperationException will be thrown. In contrast, BindingList<T>.AddNewCore can throw an InvalidCastException or NotSupportedException.
- FastBindingListT might not work properly if items can change their hash code while they are added to the collection.
![]() |
---|
Do not store elements in a FastBindingListT that may change their hash code while they are added to the collection. Finding such elements may fail even if CheckConsistency is . If hash code is derived from some identifier property or field, you can prepare a T instance by overriding the AddNewCore method or by subscribing the AddingNew event to make IBindingList.AddNew implementation work properly. |
New features and improvements compared to BindingListT:
- Disposable - The FastBindingListT implements the IDisposable interface. When an instance is disposed, then both incoming and outgoing event subscriptions (self events and PropertyChanged event of the elements) are removed. If the wrapped collection passed to the constructor is disposable, then it will also be disposed. After disposing accessing the public members may throw ObjectDisposedException.
- Overridable properties - In FastBindingListT the AllowNew, AllowRemove, AllowEdit and RaiseListChangedEvents properties are virtual.
- Find support - In FastBindingListT the IBindingList.Find method is supported. In BindingListT this throws a NotSupportedException.
- Public members for finding and sorting - FastBindingListT offers several public Find and ApplySort overloads. RemoveSort method and IsSorted/SortProperty properties are also public instead of explicit interface implementations.
- New virtual members - AddIndexCore and RemoveIndexCore methods can be overridden to implement IBindingList.AddIndex and IBindingList.RemoveIndex calls.
![]() |
---|
FastBindingListT does not implement sorting. See the derived SortableBindingListT class for an IBindingList implementation with sorting support. |
See Also