简体   繁体   中英

What is this C# construct doing?

I have seen on another post this and its confusing me...

public class MyClass : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;  

   protected void NotifyPropertyChanged(String info)
   {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
   }  

   public string MyProperty
   {
       set
       {
           if (_myProperty != value)
           {
               _myProperty = value;
               NotifyPropertyChanged("MyProperty");
           }
       }
   }
}

MyClass myClass = new MyClass();

myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;
};

I'm wondering about the last few lines are doing to be honest, why would the user be assiging a delegate to an event? Would't they assign a method to it (as an event handler) or even an anonymous method as the event handler?

I thought that events were meant to encapsulate delegates.....?

You always subscribe to an event (or unsubscribe from it) using a delegate. Even if you do:

button.Click += HandleButtonClick;

that's equivalent to

button.Click += new EventHandler(HandleButtonClick);

When you say:

Would't they assign a method to it (as an event handler) or even an anonymous method as the event handler?

That's exactly what the last few lines of code do . That's what delegate (...) { ... } is.

I thought that events were meant to encapsulate delegates.....?

Events provide an implementation of the observer pattern, using delegates as the observers.

Description

This class implements the INotifyPropertyChanged interface

MSDN Notifies clients that a property value has changed.

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

This is used for example on controls like the Datagrid. It signals the control that the property has changed and the control should rerender.

About the Event

You always subscribe to an event.

MyClass myClass = new MyClass();
myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;
};

is doing the same as

MyClass myClass = new MyClass();
myClass.PropertyChanged += new PropertyChangedEventHandler(myClass_PropertyChanged);

void myClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;     
}

or

MyClass myClass = new MyClass();
myClass.PropertyChanged += myClass_PropertyChanged;

void myClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;    
}

More Information

The += adds an anonymous delegate to the event. Instead of creating a named method with the signature object sender, PropertyChangedEventArgs e , you can use C#2.0 syntax to create such delegates anonymously in the body of another function. Another way of doing it would be to use a more concise lambda syntax from C#3.5+:

myClass.PropertyChanged += (sender, e) { actual = e.PropertyName; };

This syntax was introduced in C# 2.0 They are using an anonymous method here, rather than having to create an actual instance method of the class. It's generally considered cleaner.

In C# 3 and above, a Lambda expression could have been used as well.

They are not assigning a delegate to an event, they are adding a subscriber to that event using an anonymous method.

Also, as an aside, the NotifyPropertyChanged method should be changed to:

protected void NotifyPropertyChanged(String info)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(info));
    }
}

Since there is a potential race condition between the null check and invocation of the delegate.

Technically, you are correct; events encapsulate delegates. However, event handlers themselves are delegates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM