How wrong is it to create an event handler delegate with out the standard (Obj sender, EventArgs args) signature?

前端 未结 3 494
野趣味
野趣味 2021-02-05 19:18

I understand the benefits of using the standard MS event handler delegate signature as it allows you to easily expand on the information passed through the event with out breaki

3条回答
  •  猫巷女王i
    2021-02-05 19:58

    You can do anything the wrong way if you're the only one who has to deal with it. But it's not a bad idea to learn standards and stick to them so that you keep good habits when you're working on code with others.

    So I'll make you a deal. If you promise to do it the right way, I'll give you a code snippet that'll make it much less of a pain. Just put this in a .snippet file, and put that file in:

    My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets\
    (or Visual Studio 2005 if applicable)

    And here's the snippet; use it in VS by typing ev2Generic and hitting Tab:

    
    
      
        
    Generic event with two types/arguments. ev2Generic Code snippet for event handler and On method Kyralessa Expansion
    type1 Type of the first property in the EventArgs subclass. propertyType1 arg1Name Name of the first argument in the EventArgs subclass constructor. property1Name property1Name Name of the first property in the EventArgs subclass. Property1Name type2 Type of the second property in the EventArgs subclass. propertyType2 arg2Name Name of the second argument in the EventArgs subclass constructor. property2Name property2Name Name of the second property in the EventArgs subclass. Property2Name eventName Name of the event NameOfEvent $eventName$; protected virtual void On$eventName$($eventName$EventArgs e) { var handler = $eventName$; if (handler != null) handler(this, e); }]]>

提交回复
热议问题