Tuesday, August 26, 2014

Example of Events in C Sharp

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

namespace EventsExample
{
    public delegate void EventEx();
    public class EventExample
    {
        public  event EventEx events;
        public void Display()
        {
            for (int i = 1; i <= 100; i++)
            {
                if (i%25==0 && events != null)
                {
                 
                    events();
                }
             
            }
         
        }
     
    }
    public class AnotherExample
    {
        public int Count
        { get; private set; }
        public AnotherExample(EventExample ev)
        {
            Count = 0;
            ev.events += eventexecute;
                   

        }
        public void eventexecute()
        {
            Count++;
        }
     

    }
    class Program
    {
        static void Main(string[] args)
        {
            EventExample ev = new EventExample();
            AnotherExample an = new AnotherExample(ev);
         
            ev.Display();
            Console.WriteLine("{0} Quarters in 100 ",an.Count);
        }
    }
}