Feryt
Feryt

Reputation: 2302

Run code after/before adding item to an Collection in .NET

Is it possible to run code aftter or before the item was added to collection?

Lets say i have object Product with property Price and Warehouse object with properties IList Products and TotalPrice. I need to update TotalPrice each time the collection was modified. I dont want to calculate TotalPrice inside that property each time its accessed.

If it is possible only with AOP, is there any free AOP framework for .net?

Upvotes: 0

Views: 48

Answers (1)

Addys
Addys

Reputation: 2501

Take a look at ObservableCollection<T>, which is a collection designed especially for the scenario you described:

MSDN link: "Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed."

Upvotes: 2

Related Questions