Class ObservableList<T>
- Namespace
- CarinaStudio.Collections
- Assembly
- CarinaStudio.AppBase.Core.dll
Implementation of IList<T> which also implements INotifyCollectionChanged and INotifyPropertyChanged.
public class ObservableList<T> : IList, ICollection, IList<T>, ICollection<T>, INotifyCollectionChanged, INotifyPropertyChanged, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
Type Parameters
TType of element.
- Inheritance
-
ObservableList<T>
- Implements
-
IList<T>ICollection<T>IEnumerable<T>
- Inherited Members
- Extension Methods
Constructors
ObservableList(IEnumerable<T>?)
Initialize new ObservableList<T> instance.
public ObservableList(IEnumerable<T>? elements = null)
Parameters
elementsIEnumerable<T>Initial elements.
ObservableList(int)
Initialize new ObservableList<T> instance.
public ObservableList(int capacity)
Parameters
capacityintInitial capacity.
Properties
Capacity
Get capacity of the list.
public int Capacity { get; }
Property Value
Count
Get number of elements.
public int Count { get; }
Property Value
this[int]
Get or set element.
public T this[int index] { get; set; }
Parameters
indexintIndex of element.
Property Value
- T
Element.
Methods
Add(T)
Add element to list.
public int Add(T element)
Parameters
elementTElement to add.
Returns
- int
Index of added element in list.
AddRange(IEnumerable<T>)
Add elements to list.
public void AddRange(IEnumerable<T> elements)
Parameters
elementsIEnumerable<T>Elements to add.
AsReadOnly()
Make the list as read-only IList<T>.
public IList<T> AsReadOnly()
Returns
- IList<T>
Read-only IList<T>, the instance also implements INotifyCollectionChanged and IReadOnlyList<T>.
BinarySearch(T)
Use binary-search to find given element.
public int BinarySearch(T element)
Parameters
elementTElement to be found.
Returns
- int
Index of found element, or bitwise complement of index of proper position to put element.
Remarks
The type of element must implement either IComparable<T> or IComparable, otherwise a NotSupportedException exception will be thrown.
BinarySearch(T, IComparer<T>)
Use binary-search to find given element.
public int BinarySearch(T element, IComparer<T> comparer)
Parameters
elementTElement to be found.
comparerIComparer<T>IComparer<T> to compare elements.
Returns
- int
Index of found element, or bitwise complement of index of proper position to put element.
BinarySearch(T, Comparison<T>)
Use binary-search to find given element.
public int BinarySearch(T element, Comparison<T> comparison)
Parameters
elementTElement to be found.
comparisonComparison<T>Comparison function.
Returns
- int
Index of found element, or bitwise complement of index of proper position to put element.
BinarySearch<TKey>(TKey, Func<T, TKey>)
Use binary-search to find given element by key.
public int BinarySearch<TKey>(TKey key, Func<T, TKey> keyGetter) where TKey : IComparable<TKey>
Parameters
keyTKeyKey of element to be found.
keyGetterFunc<T, TKey>Method to get key from element.
Returns
- int
Index of found element, or bitwise complement of index of proper position to put element.
Type Parameters
TKeyType of key of element.
BinarySearch<TKey>(TKey, Func<T, TKey>, Comparison<TKey>)
Use binary-search to find given element by key.
public int BinarySearch<TKey>(TKey key, Func<T, TKey> keyGetter, Comparison<TKey> comparison)
Parameters
keyTKeyKey of element to be found.
keyGetterFunc<T, TKey>Method to get key from element.
comparisonComparison<TKey>Comparison function.
Returns
- int
Index of found element, or bitwise complement of index of proper position to put element.
Type Parameters
TKeyType of key of element.
Clear()
Clear all elements.
public void Clear()
Contains(T)
Check whether given element is contained in list or not.
public bool Contains(T element)
Parameters
elementTElement.
Returns
- bool
True if element is contained in list.
CopyTo(int, T[], int, int)
Copy elements to array.
public void CopyTo(int index, T[] array, int arrayIndex, int count)
Parameters
indexintIndex of first element in list to copy.
arrayT[]Array to place copied elements.
arrayIndexintIndex of first position in
arrayto place copied elements.countintNumber of elements to copy.
CopyTo(T[], int)
Copy elements to array.
public void CopyTo(T[] array, int arrayIndex)
Parameters
arrayT[]Array to place copied elements.
arrayIndexintIndex of first position in
arrayto place copied elements.
GetEnumerator()
Get IEnumerator<T> to enumerate elements.
public IEnumerator<T> GetEnumerator()
Returns
IndexOf(T)
Get index of given element in list.
public int IndexOf(T element)
Parameters
elementTElement.
Returns
- int
Index of element in list, or -1 if element is not contained in list.
Insert(int, T)
Insert element at given position.
public void Insert(int index, T element)
Parameters
indexintIndex of position to insert element.
elementTElement to insert.
InsertRange(int, IEnumerable<T>)
Insert elements at given position.
public void InsertRange(int index, IEnumerable<T> elements)
Parameters
indexintIndex of position to insert elements.
elementsIEnumerable<T>Elements to insert.
IsEmpty()
Check whether the list is empty or not.
public bool IsEmpty()
Returns
- bool
True if the list is empty.
IsNotEmpty()
Check whether the list is not empty or not.
public bool IsNotEmpty()
Returns
- bool
True if the list is not empty.
Move(int, int)
Move element in the list.
public void Move(int index, int newIndex)
Parameters
MoveRange(int, int, int)
Move elements in the list.
public void MoveRange(int index, int newIndex, int count)
Parameters
indexintIndex of first element to move.
newIndexintNew index of first moved element.
countintNumber of elements to move.
Remove(T)
Remove first found of given element.
public bool Remove(T element)
Parameters
elementTElement to remove.
Returns
- bool
True if element has been removed.
RemoveAll(Predicate<T>)
Remove elements which match given condition from list.
public int RemoveAll(Predicate<T> predicate)
Parameters
predicatePredicate<T>Function to check whether log matches given condition or not.
Returns
- int
Number of removed elements.
RemoveAt(int)
Remove element at given position.
public void RemoveAt(int index)
Parameters
indexintIndex of element to remove.
RemoveRange(int, int)
Remove elements.
public void RemoveRange(int index, int count)
Parameters
Reverse()
Make given list as reversed IList<T>.
public IList<T> Reverse()
Returns
- IList<T>
Reversed IList<T> which also implements INotifyCollectionChanged and IReadOnlyList<T>.
ReverseAsReadOnly()
Make given list as reversed and read-only IList<T>.
public IList<T> ReverseAsReadOnly()
Returns
- IList<T>
Reversed read-only IList<T> which also implements INotifyCollectionChanged and IReadOnlyList<T>.
ToArray()
Copy elements to array.
public T[] ToArray()
Returns
- T[]
Array of copied elements
ToArray(int, int)
Copy elements to array.
public T[] ToArray(int index, int count)
Parameters
Returns
- T[]
Array of copied elements
TrimExcess()
Set the capacity to the actual number of elements in list.
public void TrimExcess()
Events
CollectionChanged
Raised when list changed.
public event NotifyCollectionChangedEventHandler? CollectionChanged
Event Type
PropertyChanged
Raised when property changed.
public event PropertyChangedEventHandler? PropertyChanged