C# tutorial-Events in C#

Events in C#

A event in C# is a notification sent from a sender to a receiver. The sender is class or object that publishes the event and the receiver is a class or object that subscribes to the event. The events are commonly used in Window Form Applications. For example, when a user clicks on the button1, the buton1_Click() method of the Form1 class is subscribed to the event in the InitializeComponent() method by the following line:

this.button1.Click += new System.EventHandler(this.button1_Click);

The Click is an event of the button1 class. The button1 class is the sender. The Form1 class that has the button1_Click() method is the receiver. The EventHandler is a delegate contains in the System class and points to the button1_Click() method. You may click delegate to learn it. To subscribe to the event, the + sign is used. If you want to unsubscribe from the event, you can use the - sign.

In the following example, we create a sender classed called Eventtest that contains the EventE event. The Program is the receiver class that has the sum(int,int) method. The event EventE is raised in the Raise(int,int) method.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;


namespace ConsoleApplication1


    delegate void Process(int a, int b); //declaring delegate

    class Eventtest


        public static event Process EventE; //declaring event of delegate type

        public static void Raise(int a, int b)//method to raise event


            EventE(a, b);




    class Program



        delegate void Mydelegate(string  name);

        static void Main(string[] args)


            Eventtest.EventE += Program.sum; //subscribe to the event handler

            Eventtest.Raise(12, 45); //raise event

            Eventtest.EventE -= Program.sum;//unsubscribe from the event





        public static void sum(int a, int b) //This method works when the event raised



            int re;

            re = a + b;



        static void sub(int a, int b)


            Console.WriteLine(a - b);






MohitKumar comment


Check this also for c# interview Questions http://skillgun.com/sql/interview-questions-and-answers


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.