EVENTS IN C#

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  .NET 
C#
 427  View(s)
Ratings:
Rate this:
I want to use Events in C# How to use this.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: Events in C#

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!