1

What does (e, v) => syntactically mean in C#?

e.g.,

TreeViewModel root = TreeViewModel.initializeRoot(parentStatus, statuses);
root.PropertyChanged += (e, v) => updateConditions();

I know that I'm registering to listen to property changes of the root object. And, if such an event happens, then I'm calling the updateConditions() method. But, what is the (e, v) => in between?

And, is there are way to send the changed property as a parameter to updateConditions()?

mauryat
  • 1,610
  • 5
  • 29
  • 53

3 Answers3

6

It's a Lambda Expression. Basically it creates an anonymous method which calls updateConditions() and binds the event to that anonymous method.

It's equivalent to.

private void root_PropertyChanged(object e, PropertyChangeEventArgs v)
{
    updateConditions();
}

root.PropertyChanged += root_PropertyChanged
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
6

For your second question:

It is possible to send updated PropertyName into updateConditions(). Because v is of type PropertyChangedEventArgs it has PropertyName property:

root.PropertyChanged += (e, v) => updateConditions(v.PropertyName);
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

The syntax (e, v) => (some sort of expression) creates a lambda function taking parameters 'e' and 'v' and then returning the result of evaluating the expression. It's basically a way of creating a function in place, so you don't have to define a delegate and pass one in that way. In your case, it takes the arguments e and v and then simply returns the result of calling updateConditions(). Have a look at http://www.codeproject.com/Articles/24255/Exploring-Lambda-Expression-in-C for more detail on how these are used.

Animatinator
  • 410
  • 2
  • 6