CSharp - Delegate Events Definition

Introduction

C# Events formalizes the event register and fire pattern.

An event is a construct that exposes a subset of delegate features required for the broadcaster/subscriber model.

You can create an event by adding event keyword in front of a delegate member:

// Delegate definition
public delegate void ValueChangedHandler (decimal oldValue,
                                                decimal newValue);
public class ProgressBar
{
        // Event declaration
        public event ValueChangedHandler PriceChanged;
}

Code within the ProgressBar type has full access to PriceChanged and can treat it as a delegate.

Code outside of ProgressBar can only perform += and -= operations on the PriceChanged event.

Event Modifiers

Events can be virtual, overridden, abstract, or sealed.

Events can also be static:

class Test
{
    public static event EventHandler<EventArgs> StaticEvent;
    public virtual event EventHandler<EventArgs> VirtualEvent;
}

Related Topics