✅ Delegates in C#
Hello guys, I just read a bit about delegates in C#, can someone confirm whether the following statements are correct and would really appreciate if you guys can add to those statements if needed (like any things I missed).
So, what I've understood, is that a delegate is just a type that stores a reference to a method.
if we declare a delegate method like this:
We are expecting our delegate type to reference a method that will return an int and have 2 parameters of type int.
So we expect to reference a method like this:
Then we store the reference to the method as follows:
(I have one question, noticed that we don't use any constructor call when we create the object, what does that mean?)
Finally, can someone explain, why is a delegate useful pls
So, what I've understood, is that a delegate is just a type that stores a reference to a method.
if we declare a delegate method like this:
public static delegate int PerformCalculations(int x, int y);We are expecting our delegate type to reference a method that will return an int and have 2 parameters of type int.
So we expect to reference a method like this:
public static int AddNum(int num1, int num2) {...}Then we store the reference to the method as follows:
PerformCalculations myRef = AddNum; (I have one question, noticed that we don't use any constructor call when we create the object, what does that mean?)
Finally, can someone explain, why is a delegate useful pls