Question about delegates
-
Hello I had a question about named delegates. Now from my understanding named delegates can only hold references of a method that matches the signature of the delegate. I have a piece of code which is contrary to this statement. Using System; delegate int CountIt(int end); class varCapture{ static CountIt counter() { int sum = 0; CountIt ctObj = delegate(int end) { for( int i = 0; i <= end; i++ ) { Console.WriteLine(i); sum += i; } return sum; }; return ctObj; } public static void Main() { CountIt count = counter(); int result; result = count(3); } Now the delegate of type CountIt must refer to a method that takes int as a parameter and returns int as a parameter. CountIt count = counter(); In the following line the delegate count is pointing to the method with no parameters or return type. So I wanted to understand how is it possible? Is this assignment possible because the counter() is returning a object of type CountIt(same as that of the delegate)? If so when CountIt count = counter(); is executed it calls counter() and then just initializes the CountIt object an return it, does it actually go into the anonymous method block code? If not why? Thanks Revant Jain
-
Hello I had a question about named delegates. Now from my understanding named delegates can only hold references of a method that matches the signature of the delegate. I have a piece of code which is contrary to this statement. Using System; delegate int CountIt(int end); class varCapture{ static CountIt counter() { int sum = 0; CountIt ctObj = delegate(int end) { for( int i = 0; i <= end; i++ ) { Console.WriteLine(i); sum += i; } return sum; }; return ctObj; } public static void Main() { CountIt count = counter(); int result; result = count(3); } Now the delegate of type CountIt must refer to a method that takes int as a parameter and returns int as a parameter. CountIt count = counter(); In the following line the delegate count is pointing to the method with no parameters or return type. So I wanted to understand how is it possible? Is this assignment possible because the counter() is returning a object of type CountIt(same as that of the delegate)? If so when CountIt count = counter(); is executed it calls counter() and then just initializes the CountIt object an return it, does it actually go into the anonymous method block code? If not why? Thanks Revant Jain
int MyMethod() { return 9; } int n = MyMethod(); That's all your code is doing, it's no mystery.
Revant Jain wrote:
If so when CountIt count = counter(); is executed it calls counter() and then just initializes the CountIt object an return it, does it actually go into the anonymous method block code? If not why?
counter() returns an object, which is a delegate. So, this works. When the delegate fires, it will run the anonymous code, because that's how the delegate was defined. The code is convoluted, but it's correct.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )