Tags

, , , , ,

Delegates, a main question when it comes to C# interviews. When I faced the internship interview at IFS last October I was asked a question “What is a delegate” I said “It is a way to achieve thread safety”, actually I used delegates in my Imagine cup project in 2013 but when I faced that interviews, I really had a good clear concept of it. I said its thread safety because the reference material I read the day before the interview had many points on Delegates but due to my bad memory I could remember only that point even in that Imagine cup scenario really I used Delegate to avoid some code execution complexities.

Then the interviewer asked me ‘What do you mean by thread safety” so I explained it. Then again he asked me “OK, now how will you explain delegates to a newbie using the points you said”.. this is the moment I had a mini_heart_attack();

In simple words, delegates are the callback functions in C#. (I really forgot this point that time)

image_172FE501

So this JS pseudo code will explain what a callback function is,

function someAction(x, y, someCallback) {
    return someCallback(x, y);
}
 
function calcProduct(x, y) {
    return x * y;
}
 
function calcSum(x, y) {
    return x + y;
}
// alerts 75, the product of 5 and 15
alert(someAction(5, 15, calcProduct));
// alerts 20, the sum of 5 and 15
alert(someAction(5, 15, calcSum));

If you can’t read that simple code, sorry this is not for you actually. So just close this browser tab.

The equivalent to the above Javascript in C# will be something like,

 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
class Program
{
    delegate int CalcFunc(int a, int b);

    static void Main(string[] args)
    {
        CalcFunc someCalcFunc = new CalcFunc(SumFunc);
        Console.WriteLine(someCalcFunc(5,6).ToString());

        someCalcFunc = ProdFunc;
        Console.WriteLine(someCalcFunc(5, 6).ToString());

        someCalcFunc = SumFunc;
        Console.WriteLine(someCalcFunc(5, -6).ToString());

        Console.ReadKey();
    }

    static int SumFunc(int sum_a, int sum_b)
    {
        return sum_a + sum_b;
    }

    static int ProdFunc(int prod_a, int prod_b)
    {
        return prod_a * prod_b;
    }
}

Let me explain this,

Line 3 delegate int CalcFunc(int a, int b); declare a delegate CalcFunc that accepts two int parameters and return another int type variable.

Line 7, CalcFunc someCalcFunc = new CalcFunc(SumFunc); instantiate the delegate CalcFunk that calls a method we defined in Line 19, that is SumFunc which SHOULD return the same return type and the same parameter types of the delegate CalcFunc

Line 8, Console.WriteLine(someCalcFunc(5,6).ToString()); I just perform some operations with the delegate instance someCalcFunc

Line 10, I change the reference of the someCalcFunc delegate to another same return type method ProdFunc, I don’t really need to make another instance of this delegate CalcFunc for this purpose, simply someCalcFunc = ProdFunc; works well.

and later I just perform some other operations.

I hope you will be able to catch it. Happy coding 🙂

Advertisements