Search This Blog

Tuesday, August 10, 2010

Implementation of Events and Delegates in c#

Here i am providing a program to implement events and delegates in c#. In this program i have created an event as "Raisealarm" and delegate as "Alarm". Then the event is called in main() function after passing reference of event handler "fn" to delegate "Alarm".

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

namespace ConsoleApplication3
{
class Class1
{
public delegate void Alarm();
public event Alarm RaiseAlarm;

public static void fn()
{
Console.WriteLine("Alarm Raised");
}
public static void Main()
{

int time = 0;
Class1 c = new Class1();
Console.WriteLine("Enter time");
time = Convert.ToInt32(Console.ReadLine());
c.RaiseAlarm += new Alarm(fn);
if (time == 6)
{
c.RaiseAlarm();
}
}
}
}

No comments:

Post a Comment