I have a function that is to be called if a list has changed since it was last called, what would be the best way of implementing this?
ex:
List<A> OurList = new List<A>();
private void Update()
{
Boolean Changed = //?
if(Changed) CheckList(OurList);
}
I would have assumed make a variable to store the old list and compare, but how would I update the old list to the new list without copying it all out? (If I do an assign, it will update the "old list" too)
Use an ObservableCollection<T>
instead of a List, then subscribe to the CollectionChanged
event.
This way, you will be told when the list is changed instead of having to scan the data to figure out what happened after the fact.
The most efficient way would be to wrap the List<>
in your own class and have all the mutating methods (Add, Remove) set a boolean flag.
Your Method can then just look at that flag and reset it.
If you're using .NET 4.0, you could use the ObservableCollection class. (prior to 4.0, you would need to reference WPF).
Once you create your list, just add a handler to the CollectionChanged event.
Your best bet would be to use an ObservableCollection<T>
or BindingList<T>
to get push notification of a change. You could also subclass Collection<T>
if you want any custom behaviour.
To answer your question as asked (except for the cpu-intensive bit), you can use an existing feature of List<T>
: it maintains its own version internally so that it can throw if the collection has changed during enumeration.
This is based on analyzing code from reflector. It is not part of any contract, so it is liable to break at any time. It may also not work in a partial-trust environment. Please use with care:
public static int GetVersion<T>(this List<T> list)
{
return (int)list.GetType()
.GetField("_version", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(list);
}
...
private int _lastCheckedVersion = 0;
private void Update()
{
int currentVersion = ourList.GetVersion();
if(currentVersion != _lastCheckedVersion) CheckList(ourList);
_lastCheckedVersion = currentVersion;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With