You are on page 1of 4

Awesome C sharp presents : Delegates and events

by santosh

for more information please visit
Now Lets See delegate in technical terms

Friends i already defined that classes are reference type in one of my previous post of value type and reference type classes allows you to create instances of objects and use them in special ways to meet your application requirement. Another reference type is in c sharp is Delegate. Delegate allows you to change the reference to a method at runtime. This means that you can decide the execution a method at runtime, based on requirement on the application. The Method can be activated at the occurrences of an event, where an event is associated with the delegate to call the method at runtime Delegates Delegates in C sharp allows you to dynamically change the reference to the method in a class. In other words Delegate is a reference type variable which holds the reference of the method, This reference can be changed at runtime. Although Delegate is the feature which changes the calling method at runtime. Declaring Delegates Delegate refer to the methods, which have same signature as that of the delegate. Consider the following example of delegate declaration.

public delegate void Awesome_C_sharp_Delegate(string s)
Declared delegate type can be used to reference any method. This method should have a single string type parameter but does not return any value. Now Let's create a simple delegate code.

here is the code using System; class Car { public delegate void Awesome_C_sharp_Delegate(); public static void Accept() { Console.WriteLine(" Hello i m Accept function"); } public static void Display() { Console.WriteLine(" i m Display function"); } public static void Main(string[] args) { Awesome_C_sharp_Delegate d1; int num; Console.WriteLine("please enter a number"); num = Convert.ToInt32(Console.ReadLine()); if ((num > 0) && (num < 10)) { d1 = new Awesome_C_sharp_Delegate(Accept); d1(); } else if ((num > 10)) { d1 = new Awesome_C_sharp_Delegate(Display); d1(); } Console.Read(); }} In this code example we have created a integer variable num and based on number stored in that variable we have implemented conditional constructs if else in which if number is between 0-to 10 delegate will reference to Accept function and if number is greater than 10 delegate will reference the Display function.

here we have created two functions with double type parameter. and various new functions like IndexOf substring and last but not the least Length. When a user give M as a input command it will invoke Multiply function otherwise Divide function will be invoke by delegates.

Delegate type : Delegate are classified into two types Single cast and Multi cast delegate. A single cast delegate can call only one method at a time, whereas multicast delegate can call multiple function at a time. We know how to create single caste delegate. Now let's see how to create multicast delegate

code is here.... using System; class Car { public delegate void mydelegate(); public static void Accept() {

Console.WriteLine("Accept invoked"); } public static void Display() { Console.WriteLine("Display invoked"); } public static void Main() { mydelegate awesome_c_sharp = new mydelegate(Accept); awesome_c_sharp += new mydelegate(Display); awesome_c_sharp(); Console.Read(); }} Here += operator helps us to create multicast delegate. Friends soon will be posting for delegates with event keep visiting Thanks.. be happy feel freedom enjoy your coding and last thing don't forget u are awesome... take care god bless u

by santosh

for more information please visit
====================================================== all right reserved it 2013 awesome c sharp brought to u by vardana solution ======================================================