Is this the correct syntax for generic delegates and events?

I am reading the msdn library topic about genrics . There is an example of declaring an event using shared delegates, but is this true?

// Code block 8. Generic event handling

public delegate void GenericEventHandler<S,A>(S sender,A args);
public class MyPublisher
{
   public event GenericEventHandler<MyPublisher,EventArgs> MyEvent;
   public void FireEvent()
   {
      MyEvent(this,EventArgs.Empty);
   }
}
public class MySubscriber<A> //Optional: can be a specific type
{
   public void SomeMethod(MyPublisher sender,A args)
   {...}
}
MyPublisher publisher = new MyPublisher();
MySubscriber<EventArgs> subscriber = new MySubscriber<EventArgs>();
publisher.MyEvent += subscriber.SomeMethod;  // is this line correct?

Is it possible to directly apply a method to an event without first wrapping it with our delegate?

+3
source share
3 answers

Yes, this is new functionality in C # 2.0, and it will create a delegate for you. Note that you are still creating a delegate, but the creation is invisible.

+6
source
publisher.MyEvent += subscriber.SomeMethod; 

In the line above, the expression on the right side is "subscriber .SomeMethod" is of type GenericEventHandler (S sender, A args).

, , . ... .... . - , , .

# - :

publisher.MyEvent +=    
  new GenericEventHandler<MyPublisher,EventArgs>(subscriber.SomeMethod);

# , .SomeMethod. SomeMethod, MyEvent... , .

SomeMethod , - , (, SomeMethod) :

publisher.MyEvent += delegate(MyPublisher s, EventArgs a)
                             { 
                                 /* the SomeMethod Code */ 

                             };

, , :

 publisher.MyEvent += (s, a) => {/*the SomeMethod code*/};

- 's' 'a' MyEvent.... , , , .

, MyEvent lambda.

+1

, . . # 3.0

, ,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generic_Delegates_and_Events
{
    public delegate void GenericEventHandler<S,A>(S sender,A args); //generic delegate

    public class MyPublisher
    {
        public event GenericEventHandler<MyPublisher,EventArgs> MyEvent;
        public void FireEvent()
        {
            MyEvent(this,EventArgs.Empty);
        }
    }

    public class MySubscriber<A> //Optional: can be a specific type
    {
        public void SomeMethod(MyPublisher sender,A args)
        {
            Console.WriteLine("MySubscriber::SomeMethod()");
        }
    }
    public class MySubscriber2<A> //Optional: can be a specific type
    {
        public void SomeMethod2(MyPublisher sender, A args)
        {
            Console.WriteLine("MySubscriber2::SomeMethod2()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyPublisher publisher = new MyPublisher();
            MySubscriber<EventArgs> subscriber = new MySubscriber<EventArgs>();
            publisher.MyEvent += subscriber.SomeMethod;
            MySubscriber2<EventArgs> subscriber2 = new MySubscriber2<EventArgs>();
            publisher.MyEvent += subscriber2.SomeMethod2;
            publisher.FireEvent();
        }
    }
}
0

Source: https://habr.com/ru/post/1765879/


All Articles