C # listens to changes in List and Dictionary data (modification, clearing, etc.)

Previously, my job was to develop Unity, and I often needed to update the UI by listening to the data changes in lists or dictionaries. This article introduces two listening methods, of which the new modifier is commonly used.

New keyword

new In addition to calling the constructor to create objects and generic constraints, it can also be used as a modifier, which is used to Explicitly hide members inherited from the base class

 //To hide an inherited member,  declare it in the derived class by using the same member name, and modify it with the new keyword. For example: public class BaseC { public int x; public void Invoke() { } } public class DerivedC : BaseC { new public void Invoke() { } }

Listen to List

Method 1: new modifier

The principle is simple, inheritance List , using new hide Add And add your own delegate callback.

 namespace MyObservable { #Regional public entrustment (by visiting blogs) public delegate void VoidValueCallback(); public delegate void OneValueCallback<T>(T data); public delegate void TwoValueCallback<T1, T2>(T1 data1, T2 data2); #endregion public class ObserveList<T> : System.Collections.Generic.List<T> { public VoidValueCallback DOnClear; public OneValueCallback<T> DOnAdd, DOnRemove; public OneValueCallback<System.Collections.Generic.IEnumerable<T>> DOnAddRange; #Rewriting the region construction method (www.azimiao. com) public ObserveList():base() { } public ObserveList(System. Collections.Generic.IEnumerable<T> collection) : base(collection) { } public ObserveList(int num) : base(num) { } #endregion /// <summary> ///Note that removeall is not clear, and its call parameters are matching rules /// </summary> public OneValueCallback<int> DOnRemoveAll; public OneValueCallback<int> DOnRemoveAt; public TwoValueCallback<int, int> DOnRemoveRange; public new void Add(T item) { base.Add(item); DOnAdd?.Invoke(item); } public new void AddRange(System. Collections.Generic.IEnumerable<T> collection) { base.AddRange(collection); DOnAddRange?.Invoke(collection); } public new bool Remove(T item) { bool flag = base.Remove(item); if (flag) { DOnRemove?.Invoke(item); } return flag; } public new void Clear() { base.Clear(); DOnClear?.Invoke(); } public new int RemoveAll(Predicate<T> match) { int num = base.RemoveAll(match); DOnRemoveAll?.Invoke(num); return num; } public new void RemoveAt(int index) { base.RemoveAt(index); DOnRemoveAt?.Invoke(index); } public new void RemoveRange(int index, int count) { base.RemoveRange(index, count); DOnRemoveRange?.Invoke(index, count); } } }

Test code:

 class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); MyObservable.ObserveList<int> test1 = new MyObservable.ObserveList<int>(0); test1.DOnAdd += (int a) => { Console.WriteLine("add a number:" + a); }; test1.DOnRemove += (int a) => { Console.WriteLine("delete a number:" + a); }; test1.DOnClear += () =>{ Console.WriteLine("clear~"); }; test1.Add(100); test1.Remove(100); test1.Clear(); Console.ReadKey(); } }

Operation results:

 Hello World! add a number:100 delete a number:100 clear~

Method 2: Implement the IList<T>interface

Generally speaking, this interface is not very common, because it is a thankless task to implement a new List wheel that can pass the test and meet the standards, so I will just talk about the following principles.

IList<T> The generic interface is ICollection<T> The descendant of the generic interface is the base interface of all generic lists. We inherit the interface, indicating that the current class is a generic list.

After inheriting this interface, you need to implement iterator return IEnumerator It is not complicated, but unnecessary.

After you implement this class, just give it a copy of the delegation mentioned above.

Monitor Dictionary

Method 1: new modifier

Same as the List above, but slightly different.

Method 2: Implement the IDictionary<T>interface

The principle is the same as above IList<T> See:

 https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.idictionary-2

After implementing the interface, add a custom delegate.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/7308.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*