Maha

Maha

  • NA
  • 600
  • 70.8k

Event and without Event

Dec 22 2015 8:25 AM

The program with event is modify to get the same output without event. How can this situation be explained?

Without event public static event EventHandler _show; is comment out and EventHandler _show = new EventHandler(Dog); is added.

 With Event

 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();

         Console.ReadKey();

    }

     static void Cat()

    {

        Console.WriteLine("Cat");

    }

     static void Dog()

    {

        Console.WriteLine("Dog");

    }

     static void Mouse()

    {

        Console.WriteLine("Mouse");

    }

}

/*

Dog

Cat

Mouse

Mouse

 */

 Without Event

 using System;

 public delegate void EventHandler();

 class Program

{

    //public static event EventHandler _show; comment out

     static void Main()

    {

        EventHandler _show = new EventHandler(Dog); //This is added

         // 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();

         Console.ReadKey();

    }

     static void Cat()

    {

        Console.WriteLine("Cat");

    }

     static void Dog()

    {

        Console.WriteLine("Dog");

    }

     static void Mouse()

    {

        Console.WriteLine("Mouse");

    }

}

/*

Dog

Cat

Mouse

Mouse

 */


Answers (2)