A delegate is an object that can refer to a method. It means that it can hold a reference to a method. The method can be called through this reference or we can say that a delegate can invoke the method to which it refers. The same delegate can be used to call different methods during the runtime of a program by simply changing the method to which the delegate refers. The main advantage of a delegate is that it invokes the method at run time rather than compile time. A delegate type is declared using the keyword delegate. Delegate types are derived from the Delegate class.
.NET framework provides several different delegates that provides flexible options
- Action<T> : Accepts single parameter and returns no value
- Func<T,TResult> : Accepts a single parameter and returns a value of type TResult.
C# Delegate Example
Example Program for Built-in Delegates in .NET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; //Example for Builtin delegate in .NET framework public static class Program { static void Main(string[] args) { Action<string> print = (string s) => Console.WriteLine(s); //Call above print delegate print("Sample Text"); Func<int, int> square = x => x * x; //Call above square delegate int result = square(5); Console.WriteLine(result); } } |
Example: Following is the example of creating delegates in the c# programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; namespace Tutlane { // Declare Delegate public delegate void SampleDelegate(int a, int b); class MathOperations { public void Add(int a, int b) { Console.WriteLine("Add Result: {0}", a + b); } public void Subtract(int x, int y) { Console.WriteLine("Subtract Result: {0}", x - y); } } class Program { static void Main(string[] args) { Console.WriteLine("****Delegate Example****"); MathOperations m = new MathOperations(); // Instantiate delegate with add method SampleDelegate dlgt = m.Add; dlgt(10, 90); // Instantiate delegate with subtract method dlgt = m.Subtract; dlgt(10, 90); Console.ReadLine(); } } } |
Example: Declaring and Implementing a Delegate: SimpleDelegate.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
using System; // this is the delegate declaration public delegate int Comparer(object obj1, object obj2); public class Name { public string FirstName = null; public string LastName = null; public Name(string first, string last) { FirstName = first; LastName = last; } // this is the delegate method handler public static int CompareFirstNames(object name1, object name2) { string n1 = ((Name)name1).FirstName; string n2 = ((Name)name2).FirstName; if (String.Compare(n1, n2) > 0) { return 1; } else if (String.Compare(n1, n2) < 0) { return -1; } else { return 0; } } public override string ToString() { return FirstName + " " + LastName; } } class SimpleDelegate { Name[] names = new Name[5]; public SimpleDelegate() { names[0] = new Name("Joe", "Mayo"); names[1] = new Name("John", "Hancock"); names[2] = new Name("Jane", "Doe"); names[3] = new Name("John", "Doe"); names[4] = new Name("Jack", "Smith"); } static void Main(string[] args) { SimpleDelegate sd = new SimpleDelegate(); // this is the delegate instantiation Comparer cmp = new Comparer(Name.CompareFirstNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); // observe the delegate argument sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); } // observe the delegate parameter public void Sort(Comparer compare) { object temp; for (int i=0; i < names.Length; i++) { for (int j=i; j < names.Length; j++) { // using delegate "compare" just like // a normal method if ( compare(names[i], names[j]) > 0 ) { temp = names[i]; names[i] = names[j]; names[j] = (Name)temp; } } } } public void PrintNames() { Console.WriteLine("Names: \n"); foreach (Name name in names) { Console.WriteLine(name.ToString()); } } } |
Example 3: Following is the example of implementing a multicast delegate to hold the reference of multiple methods with "+"
operator in c# programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; namespace Tutlane { // Declare Delegate public delegate void SampleDelegate(int a, int b); class MathOperations { public void Add(int a, int b) { Console.WriteLine("Add Result: {0}", a + b); } public void Subtract(int x, int y) { Console.WriteLine("Subtract Result: {0}", x - y); } public void Multiply(int x, int y) { Console.WriteLine("Multiply Result: {0}", x * y); } } class Program { static void Main(string[] args) { Console.WriteLine("****Delegate Example****"); MathOperations m = new MathOperations(); // Instantiate delegate with add method SampleDelegate dlgt = m.Add; dlgt += m.Subtract; dlgt += m.Multiply; dlgt(10, 90); Console.ReadLine(); } } } |