简体   繁体   中英

What does equating an EventHandler type var to a delegate mean in C#?

I consider my self new to CSharp. I have encountered this piece if code and cannot understand what it does.

I have not seen enywhere that an EventHandler is equal to a delegate ? What does that do? Of course below is an excerpt of the the complete code since I am hoping that would be enough for someone to understand the general concept of equating an Event Handler to a delegate. Thanks.

public class xyz : ViewModelBase
{
   protected EventHandler modelChanged = null;
   public xyz (  int a, int b)
   {
       this.modelChanged =
            delegate
            {
                // check for changes.
                if (this.ChangesExist == false)
                {
                    // set the flag
                    this.ChangesExist = true;

                    // append the title.
                    currentTitle += "*";

                    _changeTitle(currentTitle);
                }

            };
    }
}

I have not seen enywhere that an EventHandler is equal to a delegate ?

EventHandler is a delegate type .

public delegate void EventHandler(
    Object sender,
    EventArgs e
)

(Attributes removed for simplicity.)

In this case what you're seeing is an anonymous method - a way of creating a delegate from an "inline" block of code. Lambda expressions are a more common way of achieving that these days though. (Anonymous methods were introduced in C# 2; lambda expressions were introduced in C# 3.) Both lambda expressions and anonymous methods can be converted to delegate instances; some lambda expressions can also be converted into expression trees for code which needs to analyze the logic within the expression.

If you're reasonably new to delegates in general, you might also want to read my article on delegates and events .

An EventHandler is a delegate with a specific signature:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public delegate void EventHandler(
    Object sender,
    EventArgs e
)

If you're confused by the equal sign, it's not equating the handler to a delegate, it's assigning the delegate to the modelChanged property.

If you think of it like it's C/C++ modelChanged is essentially a function pointer, in C# that type of functionality is provided through delegates. There are several types of delegates and you can read about them if you'd like on msdn but for the sake of brevity I won't get into that. Essentially you're declaring an EventHandler which is a type of delegate. Then inside of xyz you're actually defining the method and assigning it to modelChanged . Later on in your code if you call modelChanged it will cause the code assigned to it in xyz to be executed.

EventHandler is just a type of delegate declared by microsoft, as such it can be used as any other delegate, you can have an instance of it in your classes and to assign implementation using anonymous delegates like in your code. It's not a best practice to use it like this because it has some special purpose intended to it by MS.

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