Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Question about delegates

Question about delegates

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Revant Jain
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • R 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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups