1

Please tell me what's the different between Class1 and Class2 or the different between Event and Delegate ? I test my class in Form1 and have the same result

//Class1.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

class Class1EventArgs
{
    public StringBuilder CallStack;

    public Class1EventArgs() {
        CallStack = new StringBuilder();
    }
}

delegate void Class1EventHandler(Object sender, Class1EventArgs e);

class Class1
{
    public event Class1EventHandler EventDelegate;

    public void EventCaller()
    {
        Class1EventArgs e = new Class1EventArgs();
        e.CallStack.AppendLine("EventCaller");
        OnClass1Event(e);
    }

    protected virtual void OnClass1Event(Class1EventArgs e)
    {
        if (EventDelegate != null)            
        {               
            EventDelegate(this, e);
            System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
        }
    }

}

//Class2.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

public class Class2EventArgs
{
    public StringBuilder CallStack = new StringBuilder();
}

public delegate void Class2EventHandler(object sender,Class2EventArgs e);

class Class2
{
    public Class2EventHandler EventDelegate;

    public Class2()
    {
        EventDelegate = new Class2EventHandler(this.OnEventHappen);
    }

    public void EventCaller()
    {
        Class2EventArgs e = new Class2EventArgs();

        e.CallStack.AppendLine("EventCaller");

        EventDelegate.Invoke(this, e);

        System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
    }

    protected virtual void OnEventHappen(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("OnEventHappen");
    }

}

//Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

public partial class Form1 : Form
{
    Class1 c1 = new Class1();
    Class2 c2 = new Class2();

    public Form1()
    {
        InitializeComponent();
    }

    #region "Button1"      

    private void button1_Click(object sender, EventArgs e)
    {
        c1.EventCaller();
    }

    private void c1_EvenDelegate(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate");
    }

    private void c1_EvenDelegate_2(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate_2");
    }

    #endregion

    private void Form1_Load(object sender, EventArgs e)
    {
        //c1 = new Class1();
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate);
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate_2);

        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen1);
        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen2);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        c2.EventCaller();
    }

    private void c2_OnEventHappen1(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen1");
    }

    private void c2_OnEventHappen2(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen2");
    }
}
Toshio
  • 13
  • 2
  • possible duplicate of [What is the difference between a delegate and events?](http://stackoverflow.com/questions/29155/what-is-the-difference-between-a-delegate-and-events) – Gishu Nov 17 '10 at 03:59
  • Refer to the question in previous comment. In short, an event is just a delegate with an gate controlled by the declaring type. The declaring type controls when the event is fired (delegates can be fired by anyone). It can also control the add to delegate and remove from delegate operations and have additional code in there. – Gishu Nov 17 '10 at 04:01

1 Answers1

2

Event is a special system stack-like variable which holds pointer(s) to functions. An event can hold a only special type of functions as its defined by a delegate. Delegate is type for a function - it's like a definition of what a function signature must look like: this function must have 2 int parameters. This is what basically a delegate means to the system. When you instantiate a delegate with a function which matches its description the instance of the delegate now holds address of that function. Is this clear somewhat or not yet ... ?:) This is how I see it.

So basically an event holds a pointer to pre-defined void type of function(the delegate type).

ivo s
  • 144
  • 3
  • No problem:) But the important thing to realize is that almost everything in computers is simply data structure (classes in .Net)! So event is a data structure, delegate is data structure, integer is a data structure (in .Net is struct actually!) and so on. Just that some are predefined (events for example by the environment -.Net in your case) and some are not (your classes & the delegate!). And some have 'native' support from the OS/environment like events for example. Somebody just wrote the event class before you and you are using it just like somebody would use your classes eventually:) – ivo s Nov 17 '10 at 04:10
  • thank you for more information. i'll go on learning about this to write the code that's give more performance and easy to understand about event and delegate – Toshio Nov 17 '10 at 04:41