Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need Assistance With This Syntax
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01462114
Message ID:
01462139
Vues:
54
>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.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform