-1

Consider the following example from dotnetperls:

using System;

public delegate void EventHandler();

class Program
{
    public static event EventHandler _show;

static void Main()
{
// Add event handlers to Show event.
_show += new EventHandler(Dog);
_show += new EventHandler(Cat);
_show += new EventHandler(Mouse);
_show += new EventHandler(Mouse);

// Invoke the event.
_show.Invoke();
}

static void Cat()
{
Console.WriteLine("Cat");
}

static void Dog()
{
Console.WriteLine("Dog");
}

static void Mouse()
{
Console.WriteLine("Mouse");
}
}

what is the point of using static modifier?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Saeid
  • 691
  • 7
  • 26

1 Answers1

1

Since you are making the event subscription from a static method (Main), this allows you to directly use static methods as event handlers.

Otherwise you would have needed an instance of the class:

using System;

public delegate void EventHandler();

class Program
{
    public static event EventHandler _show;

    static void Main()
    {
        var program = new Program();

        _show += new EventHandler(program.Dog);
        _show += new EventHandler(program.Cat);
        _show += new EventHandler(program.Mouse);
        _show += new EventHandler(program.Mouse);

        // Invoke the event.
        _show.Invoke();
    }

    void Cat()
    {
        Console.WriteLine("Cat");
    }

    void Dog()
    {
        Console.WriteLine("Dog");
    }

    void Mouse()
    {
        Console.WriteLine("Mouse");
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928