mashinkata mashinkata - 21 days ago 5
C# Question

C# Detect when Pop is called on Stack structure

I'm looking for a way to check when

Pop()
function is called on my
Stack
structure I'm currently using a property with extended setter however
.Pop()
doesn't seems to trigger the setter and I'm pretty much unable to determine when the collection is actually changed unless I'm changing reference itself. Any tips on this one ? Again I only care about the
Pop()
function I don't really want to know when Push is invoked or anything else that can modify the collection.

Answer

You'll have to create your own collection (most likely just wrapping a Stack reference) so that you can create an event and trigger it when a given operation (in this case, any collection change by the sounds of it) is performed. Other than your even logic, you can just create a member for each public member of Stack and have it simply forward the call onto a private Stack instance.