Explain Delegates in detail | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Explain Delegates in detail

28th Nov 2016, 10:41 PM
Vic - avatar
2 Answers
+ 1
Delegate usually accepts your output from a function/lambda and delegates that as input to your 'delegate' function, it's sort of precedence, do this then from that i delegate you to do the following... where the following is what your delegate method does...
29th Nov 2016, 4:23 AM
Eric Gitangu
Eric Gitangu - avatar
https://msdn.microsoft.com/de-de/library/900fyy8e.aspx Think of them like a reference type. a variable of a reference type just points to the memory, where the values are stored. But instead the delegate points to another method. The compiler needs to know, that you want to point to a method, not to an object. That's why you have to declare them with a basic structure like: delegate double MathAction(double num); Now you either provide a normal function like: static double Double(double input) { return input * 2; } and now assign it to an instance of the delegate type with: MathAction ma = Double; or you can just assign an anonymous function to the delegate (which has the great advantage of using local variables!): MathAction ma2 = delegate(double input) { return input * input; };
29th Nov 2016, 1:15 AM
ordens - avatar