How to use Delegate?

Most of us would know what is delegate. But many us of don't use them efficient enough. In this project, I have shown different methods to use delegates. This is extremely basic example for delegate. You can use these techniques and then can build up complex logics and architectures. It helps a lot for Interface supporting/driven application i.e. embedded systems.

Introduction:

Delegate is a reference to one or more methods that can be called up by a single call. It is like a 'Function Pointer ' of 'C' Language. Delegate is method without code.

Code: I have made for classes delegatesExample1, delegatesExample2, delegatesExample3 and delegatesExample4 which is having start method.

1) Main thread

staticvoid Main(string[] args)

{

newdelegatesExample1().Start();

newdelegatesExample2().Start();

newdelegatesExample3().Start();

newdelegatesExample4().Start();

Console.ReadKey();

}

Here, I have called start method of each example class. Now I'll be explaining each class one by one.

2) Example 1

classdelegatesExample1

{

delegatevoidMyMethod(String str);

public delegatesExample1()

{

Console.WriteLine("Delegate Example 1 Starts");

}

publicvoid Start()

{

MyMethod methodsToCall = null;

methodsToCall += newMyMethod(reverseString);

methodsToCall += newMyMethod(uppercaseString);

methodsToCall("Nilay");

}

void reverseString(String str)

{

char[] arr = str.ToCharArray();

Array.Reverse(arr);

Console.WriteLine("Reverse: " + newstring(arr));

}

void uppercaseString(String str)

{

Console.WriteLine("Uppercase: " + str.ToUpper());

}

}

In this class I have first defined delegate MyMethod accepting string.

Then in 'Start()' method, I'm attaching or binding methods to the delegate.

And then invoking that delegate instance which will subsequently call all the methods attached to that delegate. In this example method 'reverseString' and 'uppercaseString' will be called up.

3) Example 2

classdelegatesExample2

{

delegatevoidMyMethod(String str);

eventMyMethod MyMethodEvent = null;

public delegatesExample2()

{

Console.WriteLine("\nDelegate Example 2 Starts");

}

publicvoid Start()

{

MyMethodEvent += (reverseString);

MyMethodEvent += (uppercaseString);

MyMethodEvent("Nilay");

//FOLLOWING CODE CAN ALSO BE USED

//TO CALL ALL THE METHODS RELATED TO EVENT

MyMethodEvent.Invoke("Joshi");

}

void reverseString(String str)

{

char[] arr = str.ToCharArray();

Array.Reverse(arr);

Console.WriteLine("Reverse: " + newstring(arr));

}

void uppercaseString(String str)

{

Console.WriteLine("Uppercase: " + str.ToUpper());

}

}

In previous example we have created delegate instance and we were directly invoking that instance.

But In this example I have created an event for my delegate. This is a common way to expose an event for custom controls. An instance of an event has been created and been invoked.

4) Example 3

classdelegatesExample3

{

delegatevoidMyMethod(String str);

public delegatesExample3()

{

Console.WriteLine("\nDelegate Example 3 Starts");

}

publicvoid Start()

{

extraProcessing(reverseString);

extraProcessing2(uppercaseString, reverseString);

extraProcessing3(delegate(String str)

{

Console.WriteLine("Method defined in function call:");

Console.WriteLine(str);

}

);

MyMethod methodObj = null;

methodObj += newMyMethod(reverseString);

methodObj += newMyMethod(uppercaseString);

extraProcessing3(methodObj);

}

void extraProcessing(MyMethod method1)

{

Console.WriteLine("Passed function as a parameter: ");

method1("Nilay");

}

void extraProcessing2(MyMethod meth1, MyMethod meth2)

{

Console.WriteLine("Passed 2 Functions as a Parameter:");

Console.WriteLine("1st Function:");

meth1("Nilay");

Console.WriteLine("2st Function:");

meth2("Joshi");

}

void extraProcessing3(MyMethod method1)

{

method1("nj");

}

void reverseString(String str)

{

char[] arr = str.ToCharArray();

Array.Reverse(arr);

Console.WriteLine("Reverse: " + newstring(arr));

}

void uppercaseString(String str)

{

Console.WriteLine("Uppercase: " + str.ToUpper());

}

}

This is very interesting example. In this example delegate is passed as a parameter to the function.

In extraProcessing1 and extraProcessing2, I have passed method name directly.

In extaProcessing3, using delegate I defined Anonymous method and passed it as a parameter.

In last, againg I made instance of a delegate and attached two methods to that instance and passed that instance to the function.

5) Example 4

classdelegatesExample4

{

delegateStringMyMethod(String str);

public delegatesExample4()

{

Console.WriteLine("\nDelegate Example 4 Starts");

}

publicvoid Start()

{

CallMe( delegate(string x)

{

return x.ToLower();

}

);

}

void CallMe(MyMethod meth1)

{

Console.WriteLine("Hello " + meth1("Nilay"));

}

}

This example shows another use of a delegate returning value.

In this example, I have shown how to use delegate returning a value.

Closing Notes:

I hope this these examples have cleared basic idea of delegate. Please provide me your feedback about my article or any other suggestions.