简体   繁体   中英

What is the purpose of the arguments Object o and EventArgs ea, in ddlChooseReport_Function (ASP.net)?

I'm curious as to the purpose of this method signature in ASP:

protected void ddlChooseReport_Function(Object sender, EventArgs e) 

because in the method body afterward, it never mentions either parameter

The method signature has to be compatible with EventHandler in order to be wired up as an event handler for an event. It's a bit like implementing an interface.

Just because the particular implementation doesn't happen to use them doesn't mean they're useless. For example, you might use the same event handler to wire up events for many controls, and differentiate between them using the sender parameter - and additional information about the event can be propagated through the e parameter... although when it's just EventArgs , there isn't much more that can be propagated.

Event handler delegate types in .NET in general (not specific to ASP.NET) follow a pattern of using Object sender as the first parameter, meaning the object responsible for the event occurring. The second parameter is either of EventArgs or a subclass (eg KeyEventArgs ) to provide more information. Although it's a bit odd to have EventArgs (which contains pretty much no information) in some event handler delegate types, it does mean that you can register a "general purpose" event handler which can handle any event conforming to the pattern.

(Routed events in WPF are slightly different, IIRC, but you should read up on those separately.)

A book that I'm reading (C# 5.0 In A Nutshell) addressed this issue. Although there is no change in the method signature, it made use of EventArgs.Empty in actually invoking the delegate and left out the designation of the type used by EventHandler.

public class Stock
{
    string symbol;
    decimal price;

    // constructor
    public Stock (string symbol) { this.symbol = symbol; }

    public event EventHandler PriceChanged;

    protected virtual void OnPriceChanged
    {
        if (PriceChanged != null) PriceChanged(this, e);
    }

    public decimal Price
    {
        get { return price; }
        set
        {
            if (price == value) return;
            price = value;
            OnPriceChanged(EventArgs.Empty);
        }
    }
}

By doing this, you are merely raising the event, but disregarding any information about the event, and it avoids unnecessarily instantiating an instance or EventArgs or a subclass thereof.

This doesn't change the fact that you still have to retain the normal signature, but it presumably makes the execution quicker.

This is overall .Net framework guideline on how event method signatures should look like. Some events use arguments (like file watcher), some don't (like in your case).

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