In most cases, when we use MVVM, we use the INotifyPropertyChanged interface to provide notifications to the bindings, and the general implementation looks like this:
public class MyClass : INotifyPropertyChanged {
This works great for me whenever I read code from experts - they write similar code:
public class MyClass : INotifyPropertyChanged {
I would like to know what exactly is the reason for creating a temporary object for the PropertyChanged event.
Is this just good practice, or are there any other benefits associated with it?
I found an answer with John's answer and an explained example:
Understanding C #: raising events using a temporary variable
Here is a sample code to figure this out:
using System; using System.Collections.Generic; using System.Threading; class Plane { public event EventHandler Land; protected void OnLand() { if (null != Land) { Land(this, null); } } public void LandThePlane() { OnLand(); } } class Program { static void Main(string[] args) { Plane p = new Plane(); ParameterizedThreadStart start = new ParameterizedThreadStart(Run); Thread thread = new Thread(start); thread.Start(p); while (true) { p.LandThePlane(); } } static void Run(object o) { Plane p = o as Plane; while (p != null) { p.Land += p_Land; p.Land -= p_Land; } } static void p_Land(object sender, EventArgs e) { return; } }
Jsj
source share