Example Program using Delegates and Events in C#


 A Delegate is a type that references a method. if a delegate references only one method it is called Single Cast Delegate and if delegate references more than one method it is called Multi Cast delegate.

When a Multi Cast Delegate is called, all the methods that are referenced by Multi Cast delegate are called.

An event should be declared as a delegate type.

The below example program in C#, demonstrates implementing a multi cast delegate and a custom event in C#.



using System;

 

namespace ProgramCall

{

 

    //Declare delegate outside the class

    public delegate void mydelegate(int x, int y);

 

    class MyClass

    {

        //Declare a custom event

        public static event mydelegate myevent;

 

        public static void sum(int x, int y)

        {

            Console.WriteLine("Sum " + (x + y));

        }

 

        public static void diff(int x, int y)

        {

            Console.WriteLine("Diff " + (x - y));

        }

 

        //Raising event

        public static void RaiseEvent(int x, int y)

        {

            if (myevent != null)

            {

                myevent(x, y);

            }

        }

 

    }

 

 

    class MainClass

    {

        static void Main(string[] args)

        {

 

            //Instantiate delegate by passing a method name

            mydelegate del = new mydelegate(MyClass.sum);

 

            //attaching a method to delegate

            del += MyClass.diff;

 

            //As the above delegate holds reference of 2 methods ,

            //it is called as multicast delegate

 

            //calling a delegate

            del(10, 8);

 

            // alternate way to invoke a delegate

            del.Invoke(10, 10);

 

            //attaching a delegate to event

            MyClass.myevent += del;

 

            MyClass.RaiseEvent(10, 6);

 

            Console.Read();

        }

    }

}




Output
Sum 18
Diff 2

Sum 20
Diff 0

Sum 16
Diff 4


Zooming in Winforms Webbrowser Control in .NET
Filtering Datatable with Select Conditions and Sorting in C#.NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Modify XML file with tagname in c#.net
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Difference between Const and ReadOnly
Example for Property Change Notification in C#.NET
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Copy Datagridview cell value using ContextMenu
Inserting data in Datatable in C#
Cookie in .NET
Using Control's Child Index and Dock property for achieving desired layout in a form.
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net




Exmaple program for Delegates and Events, Raising Events in C# , MultiCast Delegate example progarm in C#.NET, Raising Custom Events in .NET, declaring custom events in C#