none
Difference between Delegate and Event RRS feed

  • Question

  • I'm confused of the difference between Delegate and Event.

    I searched several sites where mentioning about this issue but still I can't figure out the difference exactly.

    For example, we can change a data of a form from another form using delegate and event as well.

    Then, when we should use a delegate and should do a event?

    Monday, October 8, 2018 2:09 AM

Answers

  • Hi Jeff,

    Basically, the difference is:
     
    1) An Event gets raised/fired by some of your code, when you want to be able to have another part of your code do something when the event happens.
    2) A Delegate is used to specify the code that will run when that event happens (the event handler).

    The two bits of code (1.raising/firing an event and 2.specifying what to do after the event happens) can be in different classes (Form, UserControl, whatever) or it can all be in the same class ... it doesn't matter.

    Maybe my blog post on Custom Events will help. The post doesn't offer any "definitions" of event vs delegate, but in showing how to create a Custom event, I think it helps explain the difference between the two:
    https://geek-goddess-bonnie.blogspot.com/2010/03/custom-events.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, October 8, 2018 2:05 PM
    Monday, October 8, 2018 4:54 AM
    Moderator
  • If you have a delegate (myDelegate) and an event (myEvent), such as:

    public class MyClass

    {

        public delegate void MyDelegate();

     

        public MyDelegate myDelegate;

        public event MyDelegate myEvent;

    }

     

    then  you can perform various operations with the delegate:

     

    MyClass c = new MyClass();

    c.myDelegate = null;

    c.myDelegate = SomeHandler;

    c.myDelegate += SomeHandler;

    c.myDelegate -= SomeHandler;

    if(c.myDelegate != null)

    {

        c.myDelegate();

    }

     

    but you cannot perform some of operations with event:

     

    c.myEvent = null; // ERROR

    c.myEvent = SomeHandler; // ERROR

    c.myEvent += SomeHandler; // OK

    c.myEvent -= SomeHandler; // OK

    if( c.myEvent != null ) // ERROR

    {

        c.myEvent(); // ERROR

    }

     

    However, you can perform any operation with events inside a member function of MyClass.

    Therefore, the events include certain protection against some operations, which are only allowed to the owner of the events.



    • Edited by Viorel_MVP Monday, October 8, 2018 5:21 AM
    • Marked as answer by Jeff0803 Monday, October 8, 2018 2:12 PM
    Monday, October 8, 2018 5:17 AM

All replies

  • Hi Jeff,

    Basically, the difference is:
     
    1) An Event gets raised/fired by some of your code, when you want to be able to have another part of your code do something when the event happens.
    2) A Delegate is used to specify the code that will run when that event happens (the event handler).

    The two bits of code (1.raising/firing an event and 2.specifying what to do after the event happens) can be in different classes (Form, UserControl, whatever) or it can all be in the same class ... it doesn't matter.

    Maybe my blog post on Custom Events will help. The post doesn't offer any "definitions" of event vs delegate, but in showing how to create a Custom event, I think it helps explain the difference between the two:
    https://geek-goddess-bonnie.blogspot.com/2010/03/custom-events.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, October 8, 2018 2:05 PM
    Monday, October 8, 2018 4:54 AM
    Moderator
  • If you have a delegate (myDelegate) and an event (myEvent), such as:

    public class MyClass

    {

        public delegate void MyDelegate();

     

        public MyDelegate myDelegate;

        public event MyDelegate myEvent;

    }

     

    then  you can perform various operations with the delegate:

     

    MyClass c = new MyClass();

    c.myDelegate = null;

    c.myDelegate = SomeHandler;

    c.myDelegate += SomeHandler;

    c.myDelegate -= SomeHandler;

    if(c.myDelegate != null)

    {

        c.myDelegate();

    }

     

    but you cannot perform some of operations with event:

     

    c.myEvent = null; // ERROR

    c.myEvent = SomeHandler; // ERROR

    c.myEvent += SomeHandler; // OK

    c.myEvent -= SomeHandler; // OK

    if( c.myEvent != null ) // ERROR

    {

        c.myEvent(); // ERROR

    }

     

    However, you can perform any operation with events inside a member function of MyClass.

    Therefore, the events include certain protection against some operations, which are only allowed to the owner of the events.



    • Edited by Viorel_MVP Monday, October 8, 2018 5:21 AM
    • Marked as answer by Jeff0803 Monday, October 8, 2018 2:12 PM
    Monday, October 8, 2018 5:17 AM
  • I'm sorry but still I can't figure it out.

    Here is the sample code.

    <Form1>

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.RequestLabelChange += changelabelmethod;
                f2.RequestLabelChange += SoundsBeep;
                f2.ShowDialog(); 
            }
            void changelabelmethod(string newText)
            {
                this.label1.Text = newText;
            }
            void SoundsBeep(string newText)
            {
                System.Media.SystemSounds.Beep.Play();
            }
        }

    <Form2>

    public partial class Form2 : Form
        {
            public delegate void RequestLabelChangeDelegate(string newText);
            public event RequestLabelChangeDelegate RequestLabelChange;
            
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                RequestLabelChange(textBox1.Text);
            }
        }

    This code works well.

    public event RequestLabelChangeDelegate RequestLabelChange;

    And following code works as well.

    public RequestLabelChangeDelegate RequestLabelChange;

    What can we extract the difference between these two?

    Monday, October 8, 2018 2:12 PM