Suppose I have some event in a class:
class SomeClass{
event ChangedEventHandler SomeDoubleChanged;
double SomeDouble;
}
with:
delegate void ChangedEventHandler(double d);
Now suppose I want to listen for change events on SomeDouble, but only want to trigger changes greater than delta. Now I could do something like
SomeObject.SomeDoubleChanged += (d) => {if(abs(d-old_d) > delta){
//do something
};
But I want my event to handle this, so in the best case I want to do something like:
SomeObject.SomeDoubleChange += (delta, (d) => {});
and still allow:
SomeObject.SomeDoubleChange += (d) => {};
The only way I only way I thought of implementing this, is to drop the whole event keyword and implement a container with += and -= operators, operating on the specified delegates. But in my opinion this is not a very elegant solution, as it gives the user of SomeClass the idea that SomeDoubleChanged is no event.
What would be the most elegant solution to this problem?
(I’d recommend not using the term “lambda” here given that you’re also using lambda expressions. It sounds like you’re interested in the change, i.e. the delta.)
You could create your own static methods to create appropriate delegates:
Then you can use:
Unfortunately you can’t use extension methods on lambda expressions, which would make this easier.