Home > DeveloperSection > Forums > Events in C#
Ankit Singh

Total Post:341

Points:2389
Posted on    December-21-2015 10:17 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 206  View(s)
Rate this:
I want to use Events in C# How to use this.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    December-21-2015 10:19 PM

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


Don't want to miss updates? Please click the below button!

Follow MindStick