delegates and events

2019-04-01 13:13发布

I have created a very simple dummy program to understand Delegates and events. In my below program I am simple calling a method. When I call a method, five methods are automatically called with the help of delegates and events.

Kindly take a look at my program and do let me know where I am wrong or right as this is my first time using 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();
        }
    }
}

标签: c# delegates
3条回答
Deceive 欺骗
2楼-- · 2019-04-01 14:00

This is a common pattern with dealing with events:

// 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);
    }
}
查看更多
We Are One
3楼-- · 2019-04-01 14:14

Events are implemented using Delegates. That said by convention events take the form of:

 void EventHandler(Object sender, EventArgs args);

EventHandler is actually a delegate defined in .Net. EventArgs is a class in .Net that acts as a placeholder to pass additional information. If you have additional information you would create a class that derived from EventArgs and contained properties for the additional data; therefore you would create your own delegate like so:

void MyEventHandler(Object sender, MyEventArgs args);

Microsoft has a tutorial on events here and also describes defining and raising events here

查看更多
何必那么认真
4楼-- · 2019-04-01 14:14

Try putting the line

public delegate void MyFirstDelegate();

inside the Test class.

Also, use the Invoke function on the event instead, i.e.

myFirstDelegate.Invoke();
查看更多
登录 后发表回答