I have a generic callback function like this to notify listeners when a value has changed:
public Func<T, T, Task> ValueChangedAsync { get; set; }
where the first T should be the old value and second T should be the new value.
I will use this callback like this:
await ValueChangedAsync(old, newValue);
Can I make users of this function aware of which T is the old value and new value? When using the callback this is what gets implemented by default:
private Task ValueChangedAsync(string s, string s1)
{
throw new NotImplementedException();
}
I would really like the "s" to be named "oldValue" and "s1" to be named "newValue" or such.
You could do this with events and a custom delegate type, where the delegate type documentation can indicate the old value and new value parts. For example:
// As we want a non-void return type, we're not following the event conventions
// anyway... so we might as well have three parameters...
public delegate Task ValueChangedHandler<T>(object source, T oldValue, T newValue);
Then:
public event ValueChangedHandler<string> ValueChanged;
Invoke it as:
ValueChangedHandler<string> handler = ValueChanged;
if (handler != null)
{
var allHandlers = handler.GetInvocationList();
foreach (ValueChangedHandler<string> individualHandler in allHandlers)
{
await individualHandler(this, oldValue, newValue);
}
}
See more on this question at Stackoverflow