none
The differences between delegate and event RRS feed

  • 问题

  • Hello,everyone....I'm studying the differences between delegate and event in C#...

    Now,I know how to use delegate in C# code,but I really don't kown when we should use event,Is there any advance when we use event?

    Who can introduce me about the differences between the delegate and event?thank you,forgive my poor Enlish...

    2011年8月16日 5:38

答案

  • Hello,everyone....I'm studying the differences between delegate and event in C#...

    Now,I know how to use delegate in C# code,but I really don't kown when we should use event,Is there any advance when we use event?

    Who can introduce me about the differences between the delegate and event?thank you,forgive my poor Enlish...

    Delegate is a very special type defined in C# for calling different functions or methods with the same parameters in type,order and number in the specific delegate itself.

    Here's an example:

    namespace CSharp
    {
        class A
        {
            public delegate void MyDelegate();

            public void CallMyDelegate(MyDelegate md)
            {
                md();
            }
        }
        class Program
        {
            public void ProgramSay()
            {
                Console.WriteLine("This is my program");
            }

            static void Main(string[] args)
            {
                A a = new A();
                a.CallMyDelegate(new Program().ProgramSay);

            }
        }
    }

    From the sample you can see that ProgramSay is a method with the same parameters in order, number and type, just like what we've defined in the delegate "MyDelegate". So what the delegate surprised us is that a delegate can call such methods dynamically, no matter where the function defined, even if the two classes have no relation with each other at all.

    Now let's switch to event——

    namespace CSharp
    {
        class A
        {
            public delegate void MyDelegate();

            public event MyDelegate OnSayEventHandler = null;

            public void CallMyDelegate()
            {
                OnSayEventHandler();
            }
        }
        class Program
        {
           
            static void Main(string[] args)
            {
                A a = new A();
                a.OnSayEventHandler += new A.MyDelegate(a_OnSayEventHandler);
                a.CallMyDelegate(); //This will raise an event.
            }

            static void a_OnSayEventHandler()
            {
                Console.WriteLine("Program is saying....");
            }
        }
    }


    You will find that "a.OnSayEventHandler += new A.MyDelegate(a_OnSayEventHandler);" what's that? In fact, compared with the first sample we know that a delegate is only a type, it needs an instance, just we must use new to create and assign specific memory for the class. So the statement is just dynamically claim the memory for the delegate for the instance of "a". And " a.CallMyDelegate();" will raise a method defined in the class A, which will also raise the event "OnSayEventHandler", and because the "OnSayEventHandler" is pointing to " static void a_OnSayEventHandler()
    ". So that's the reason why "Program is saying...." is outputted.

    All in all, delegate is the type of event, and event is used for calling functions between two different classes, and its ancestor is "Observer mode" in OOP design.


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年8月16日 6:14
    版主