Class ObservableList<T>
Represents a list that notifies subscribers when items are added or removed.
Inheritance
Namespace: UniUtils.Data
Assembly: cs.temp.dll.dll
Syntax
public class ObservableList<T> : IEnumerable<T>
Type Parameters
Name | Description |
---|---|
T | The type of elements in the list. |
Examples
ObservableList<string> observableList = new ObservableList<string>();
observableList.OnAdd += item => Debug.Log($"Added: {item}");
observableList.OnRemove += item => Debug.Log($"Removed: {item}");
observableList.Add("Hello"); // Output: Added: Hello
observableList.Add("World"); // Output: Added: World
observableList.Remove("Hello"); // Output: Removed: Hello
observableList.Clear(); // Output: Removed: World
Properties
Count
Gets the number of elements contained in the list.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Item[Int32]
Gets the element at the specified index.
Declaration
public T this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | The zero-based index of the element to get. |
Property Value
Type | Description |
---|---|
T | The element at the specified index. |
Methods
Add(T)
Adds an item to the list and triggers the OnAdd event.
Declaration
public void Add(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to add to the list. |
Clear()
Removes all items from the list and triggers the OnRemove event for each item.
Declaration
public void Clear()
GetEnumerator()
Returns an enumerator that iterates through the list.
Declaration
public IEnumerator<T> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<T> | An enumerator for the list. |
Remove(T)
Removes the first occurrence of a specific item from the list and triggers the OnRemove event.
Declaration
public bool Remove(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to remove from the list. |
Returns
Type | Description |
---|---|
System.Boolean | true if the item is successfully removed; otherwise, false. |
Events
OnAdd
Event triggered when an item is added to the list.
Declaration
public event Action<T> OnAdd
Event Type
Type | Description |
---|---|
Action<T> |
OnRemove
Event triggered when an item is removed from the list.
Declaration
public event Action<T> OnRemove
Event Type
Type | Description |
---|---|
Action<T> |