Sobes.tech
Junior — Middle

Kuidas toimub registreerimine sündmusele, et seda saada?

sobes.tech AI

Vastus AI-lt

In C#, event registration is done using the += operator, which adds a handler (delegate) to the event. The handler is a method that will be called when the event occurs.

Example:

public class Publisher
{
    public event EventHandler MyEvent;

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

public class Subscriber
{
    public void OnMyEvent(object sender, EventArgs e)
    {
        Console.WriteLine("Event received");
    }
}

// Usage:
var publisher = new Publisher();
var subscriber = new Subscriber();
publisher.MyEvent += subscriber.OnMyEvent; // register handler

publisher.RaiseEvent(); // will call OnMyEvent

So, to receive an event, you need to subscribe to it by adding a handler method to the event.