What is the syntax to declare an event in C#?

c#, events

Solution

You forgot to mention the type. For really simple events, `EventHandler` might be enough:

public event EventHandler CollectMapsReportingComplete;

Sometimes you will want to declare your own delegate type to be used for your events, allowing you to use a custom type for the `EventArgs` parameter (see Adam Robinson's comment):

public delegate void CollectEventHandler(object source, MapEventArgs args);

public class MapEventArgs : EventArgs
{
    public IEnumerable<Map> Maps { get; set; }
}

You can also use the generic `EventHandler` type instead of declaring your own types:

public event EventHandler<MapEventArgs> CollectMapsReportingComplete;

Problem

In my class I want to declare an event that other classes can subscribe to. What is the correct way to declare the event? This doesn't work: ``` public event CollectMapsReportingComplete; ```

Original source