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
T
Type 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
elements
IEnumerable<T>Initial elements.
ObservableList(int)
Initialize new ObservableList<T> instance.
public ObservableList(int capacity)
Parameters
capacity
intInitial 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
index
intIndex of element.
Property Value
- T
Element.
Methods
Add(T)
Add element to list.
public int Add(T element)
Parameters
element
TElement to add.
Returns
- int
Index of added element in list.
AddRange(IEnumerable<T>)
Add elements to list.
public void AddRange(IEnumerable<T> elements)
Parameters
elements
IEnumerable<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
element
TElement 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
element
TElement to be found.
comparer
IComparer<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
element
TElement to be found.
comparison
Comparison<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
key
TKeyKey of element to be found.
keyGetter
Func<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
TKey
Type 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
key
TKeyKey of element to be found.
keyGetter
Func<T, TKey>Method to get key from element.
comparison
Comparison<TKey>Comparison function.
Returns
- int
Index of found element, or bitwise complement of index of proper position to put element.
Type Parameters
TKey
Type 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
element
TElement.
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
index
intIndex of first element in list to copy.
array
T[]Array to place copied elements.
arrayIndex
intIndex of first position in
array
to place copied elements.count
intNumber of elements to copy.
CopyTo(T[], int)
Copy elements to array.
public void CopyTo(T[] array, int arrayIndex)
Parameters
array
T[]Array to place copied elements.
arrayIndex
intIndex of first position in
array
to 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
element
TElement.
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
index
intIndex of position to insert element.
element
TElement to insert.
InsertRange(int, IEnumerable<T>)
Insert elements at given position.
public void InsertRange(int index, IEnumerable<T> elements)
Parameters
index
intIndex of position to insert elements.
elements
IEnumerable<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
index
intIndex of first element to move.
newIndex
intNew index of first moved element.
count
intNumber of elements to move.
Remove(T)
Remove first found of given element.
public bool Remove(T element)
Parameters
element
TElement 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
predicate
Predicate<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
index
intIndex 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