Friday, April 11, 2014

Event handler in Console application in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
    public delegate void EventDelegate();
    class EventClass
    {
        public event EventDelegate MyEvent;
        public void OnMyEvent()
        {
            if (MyEvent != null)
            {
                MyEvent();
            }
        }        
    }
    class Program
    {
        static void handler()
        {
            Console.WriteLine("Event Occurred");
        }
        static void Main(string[] args)
        {
            EventClass eventclass = new EventClass();
            eventclass.MyEvent+=handler;
            eventclass.OnMyEvent();
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment