1
  1. Generally, how shall I use the built-in generic delegate EventHandler<TEventArgs> provided by the .NET Framework?
  2. Specifically, I saw an example

    Class MyEvent{
        ...
        public event EventHandler<MyEventArgs> SomeEvent; 
        ...
    }
    
    Class EventDemo{
    
        ...
    
        void MyEventHandler(Object sender, MyEventArgs e){
            ...
        }
    
        ...
    
        myEvent = new MyEvent();
    
        myEvent.SomeEvent += new EventHandler<MyEventArgs>(MyEventHandler);
    
    }
    

    Can

    myEvent.SomeEvent += new EventHandler<MyEventArgs>(MyEventHandler);
    

    be replaced by

    myEvent.SomeEvent += MyEventHandler;
    

    In particular, does new EventHandler<MyEventArgs>() try to convert the function MyEventHandler from one type to another type?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • Your question has nothing to do with the specific delegate type, nor the generic nature of a delegate type. The difference in the syntax is simply that the compiler knows how to infer the instantiation of a delegate instance from a method group name directly. You can omit the new operator and constructor call in _any_ context where a new delegate instance is desired. See marked duplicate for details. – Peter Duniho Feb 08 '17 at 03:22

0 Answers0