Coding in C# is always fun, when you come from the VBA Excel world. There is much cool stuff, which you see and then you start wondering – why is this not implemented in VBA and why should I write plenty of code just to achieve it.
Delegates are something like this. Cool stuff in C#.
What delegates do? That’s what MSDN says about it:
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
So in my test file I have two different delegates – the first one is a void and the second one an int. The void delegate is made by the standard way a delegate is made – functions are declared, added and executed. The int delegate is a bit more interesting – we have functions again, but the delegate functions are later set in a list and a foreach loop is used with this list, in order to get the result.
This is the output:
And this is the code:
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 |
using System; using System.Collections.Generic; delegate void PrintMeInfo(int number); delegate int Calculations(int first_number, int second_number, int multiplier); class DelegateTest { public static int SumAndMultiply1(int A, int B, int C) { Console.WriteLine("Without brackets for {0}+{1}*{2}.",A,B,C); return A + B * C; } public static int SumAndMultiply2(int A, int B, int C) { Console.WriteLine("With brackets for {0}+{1}*{2}.", A, B, C); return (A + B) * C; } public static int SumAndMultiply3(int A, int B, int C) { Console.WriteLine("Just multiplying all of them.", A, B, C); return A * B * C; } public void BiggerThanTen(int number) { string result = ""; if (number>10) { result = "Bigger than 10."; } else { result = "Not bigger than 10."; } Console.WriteLine(result); } public static void IsOdd(int number) { string result = ""; if (number % 2 == 1) { result = "It is odd."; } else { result = "It is not odd"; } Console.WriteLine(result); } public static void IsDividableByTen (int number) { string result = ""; if (number % 10 == 0) { result = "Number is dividable by ten."; } else { result = "Number is not dividable by ten."; } Console.WriteLine(result); } public static void ShowMeTheNumber (int number) { Console.WriteLine("The number is {0}.",number); } static void Main() { PrintMeInfo info = null; info += new PrintMeInfo(ShowMeTheNumber); info += new PrintMeInfo(DelegateTest.IsOdd); info += new PrintMeInfo(IsDividableByTen); DelegateTest additional_info = new DelegateTest(); info += new PrintMeInfo(additional_info.BiggerThanTen); //The method is not static! Calculations calc = null; calc += new Calculations(DelegateTest.SumAndMultiply1); calc += new Calculations(DelegateTest.SumAndMultiply2); List<Calculations> my_list = new List<Calculations> { SumAndMultiply1, SumAndMultiply2 , SumAndMultiply3}; foreach (var my_result in my_list) { Console.WriteLine(my_result(5,6,7)); } info(25); } } |
Also in GitHub!
Enjoy it! 😀