Delegates and events

I created a very simple dummy program for understanding delegates and events. In my program below, I just call the method. When I call a method, five methods are automatically called using delegates and events.

Remember to take a look at my program and let me know where I am wrong or right, as this is my first time we use delegates and events.

using System;

namespace ConsoleApplication1
{
    public delegate void MyFirstDelegate();

    class Test
    {
        public event MyFirstDelegate myFirstDelegate;

        public void Call()
        {
            Console.WriteLine("Welcome in Delegate world..");
            if (myFirstDelegate != null)
            {
                myFirstDelegate();
            }
        }        
    }

    class AttachedFunction
    {
        public void firstAttachMethod()
        {
            Console.WriteLine("ONE...");
        }
        public void SecondAttachMethod()
        {
            Console.WriteLine("TWO...");
        }
        public void thirdAttachMethod()
        {
            Console.WriteLine("THREE...");
        }
        public void fourthAttachMethod()
        {
            Console.WriteLine("FOUR...");
        }
        public void fifthAttachMethod()
        {
            Console.WriteLine("FIVE...");
        }
    }

    class MyMain
    {
        public static void Main()
        {
            Test test = new Test();
            AttachedFunction attachedFunction = new AttachedFunction();
            test.myFirstDelegate += new MyFirstDelegate(attachedFunction.firstAttachMethod);
            test.myFirstDelegate += new MyFirstDelegate(attachedFunction.SecondAttachMethod);
            test.myFirstDelegate += new MyFirstDelegate(attachedFunction.thirdAttachMethod);
            test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fourthAttachMethod);
            test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fifthAttachMethod);

            test.Call();
            Console.ReadLine();
        }
    }
}
+3
source share
3 answers

Events are implemented using delegates. This indicates conventions in the form of:

 void EventHandler(Object sender, EventArgs args);

EventHandler , .Net. EventArgs - .Net, . , , EventArgs ; :

void MyEventHandler(Object sender, MyEventArgs args);

Microsoft ,

+2

, :

// define the delegate
public delegate void CustomEventHandler(object sender, CustomEventArgs e);

// define the event args
public class CustomEventArgs : EventArgs
{
     public int SomeValue { get; set; }

     public CustomEventArgs( int someValue )
     {
         this.SomeValue = someValue;
     }
}

// Define the class that is raising events
public class SomeClass
{
    // define the event
    public event CustomEventHandler CustomEvent;

    // method that raises the event - derived classes can override this
    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        // do some stuff
        // ...

        // fire the event
        if( CustomEvent != null )
            CustomEvent(this, e);
    }

    public void SimulateEvent(int someValue)
    {
        // raise the event
        CustomEventArgs args = new CustomEventArgs(someValue);

        OnCustomEvent(args);
    }
}

public class Main
{
    public static void Main()
    {
        SomeClass c = new SomeClass(); 

        c.CustomEvent += SomeMethod;
        c.SimulateEvent(10); // will cause event
    }

    public static void SomeMethod(object sender, CustomEventArgs e)
    {
         Console.WriteLine(e.SomeValue);
    }
}
+2

public delegate void MyFirstDelegate();

Test.

, Invoke , ..

myFirstDelegate.Invoke();
0

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


All Articles