An event can have many handlers. With the event handler syntax, we create a notification system. We attach additional methods without changing other parts of the code. This makes programs easier to maintain.
using System; namespace EventsInCSharp { public class OurClass { public delegate void OurDelegate(string message); public event OurDelegate OurEvent;
public void UseEvent(string message) { if (OurEvent != null) OurEvent(message); } } class Program { static void Main(string[] args) { GetReStart: OurClass OurClass = new OurClass(); OurClass.OurEvent += new OurClass.OurDelegate(OurClass_MyEvent);
Console.WriteLine("Please enter your name\n"); string msg = Console.ReadLine();
OurClass.UseEvent(msg); goto GetReStart;
}
static void OurClass_MyEvent(string message) { Console.WriteLine("Your Name is: {0}", message); } } }
c
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
An event can have many handlers. With the event handler syntax, we create a notification system. We attach additional methods without changing other parts of the code. This makes programs easier to maintain.
c