Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need Assistance With This Syntax
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01462114
Message ID:
01462139
Views:
55
>OK, I see. But could you step it through a bit? What happens when the event fires? It calls the code after the "=>"?
>
>
>>>I don't understand this:
>>>
>>>     this.PropertyChanged += (sender, e) =>
>>>

>>It's what's called an anonymous delegate. You can use that syntax instead of code like this:
>>
>>
>>     this.PropertyChanged += new EventHandler(MyPropertyChangedHandler);
>>
>>     private void MyPropertyChangedHandler(object sender, EventArgs e)
>>     {
>>         if (this.RowState == RowStateEnum.Unchanged && this.AllowRowStateChange)
>>             this.RowState = RowStateEnum.Update;
>>     }
>>
>>
>>~~Bonnie
>>
>>
>>
>>
>>>I have this code:
>>>
>>>
>>>namespace ConfigManager.ChangeSetEngine
>>>{
>>>     [Serializable]
>>>public partial class TblFacility : SQLEntityBaseClass, INotifyPropertyChanged
>>>{
>>>  partial void OnCreated()
>>>  {
>>>     this.PropertyChanged += (sender, e) =>
>>>     {
>>>         if (this.RowState == RowStateEnum.Unchanged && this.AllowRowStateChange)
>>>             this.RowState = RowStateEnum.Update;
>>>     };
>>>  }
>>>}
>>>}
>>>
>>>I don't understand this:
>>>
>>>     this.PropertyChanged += (sender, e) =>
>>>
The syntax is actually a bit more than an anonymous delegate since it uses a statement lambda.
//Anonymous delegate (assuming Bonnie's property types):
this.PropertyChanged += delegate(object sender, EventArgs e) {//statement block....};
//Lambda equivalent:
this.PropertyChanged += (object sender, EventArgs e) => {};
// But since the compiler can infer the input types:
this.PropertyChanged += (sender,e) => {};
'=>' can be regarded as meaning 'goes to' so, although you weren't using the inputs in the example they would be available in the statement block (and, yes, it runs the code after the =>)
The basic thinking behind this type of syntax is that, since the code in the statement block is unlikely to be used elsewhere, it avoids creating an unneccessary separate method.
Previous
Reply
Map
View

Click here to load this message in the networking platform