[c#] Creating a Custom Event

Can a custom event be created for any object method?
To do this do I just use the following syntax?:

myObject.myMethod +=new EventHandler(myNameEvent);

The following code has prompted this question:

   private void btRunProcessAndRefresh_Click(object sender,EventArgs e)
    {
        myProcess =new Process();
        myProcess.StartInfo.FileName = @"c:\ConsoleApplication4.exe";
        myProcess.Exited += new EventHandler(MyProcessExited);
        myProcess.EnableRaisingEvents =true;
        myProcess.SynchronizingObject =this;
        btRunProcessAndRefresh.Enabled =false;
        myProcess.Start();
    }

This question is related to c# events

The answer is


Based on @ionden's answer, the call to the delegate could be simplified using null propagation since C# 6.0.

Your code would simply be:

class MyClass {
    public event EventHandler MyEvent;

    public void Method() {
        MyEvent?.Invoke(this, EventArgs.Empty);
    }
}

Use it like this:

MyClass myObject = new MyClass();
myObject.MyEvent += new EventHandler(myObject_MyEvent);
myObject.Method();

You need to declare your event in the class from myObject :

public event EventHandler<EventArgs> myMethod; //you should name it as an event, like ObjectChanged.

then myNameEvent is the callback to handle the event, and it can be in any other class


Yes, provided you have access to the object definition and can modify it to declare the custom event

public event EventHandler<EventArgs> ModelChanged;

And normally you'd back this up with a private method used internally to invoke the event:

private void OnModelChanged(EventArgs e)
{
    if (ModelChanged != null)
        ModelChanged(this, e);
}

Your code simply declares a handler for the declared myMethod event (you can also remove the constructor), which would get invoked every time the object triggers the event.

myObject.myMethod += myNameEvent;

Similarly, you can detach a handler using

myObject.myMethod -= myNameEvent;

Also, you can write your own subclass of EventArgs to provide specific data when your event fires.


Declare the class containing the event:

class MyClass {
    public event EventHandler MyEvent;

    public void Method() {
        OnEvent();
    }

    private void OnEvent() {
        if (MyEvent != null) {
            MyEvent(this, EventArgs.Empty);
        }
    }
}

Use it like this:

MyClass myObject = new MyClass();
myObject.MyEvent += new EventHandler(myObject_MyEvent);
myObject.Method();

Yes you can create events on objects, here is an example;

public class Foo
{
    public delegate void MyEvent(object sender, object param);
    event MyEvent OnMyEvent;

    public Foo()
    {
        this.OnMyEvent += new MyEvent(Foo_OnMyEvent);
    }

    void Foo_OnMyEvent(object sender, object param)
    {
        if (this.OnMyEvent != null)
        {
            //do something
        }
    }



    void RaiseEvent()
    {
        object param = new object();
        this.OnMyEvent(this,param);
    }
}