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. Delegates

Delegates

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 4 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.
  • F Offline
    F Offline
    fmardani
    wrote on last edited by
    #1

    Hi, I believe i understand the concept of delegates. To put it simply, I think they are used to invoke methods in another class. What I don't quite understand is: What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method. Can you please help to understand this issue? Thanks

    C N 2 Replies Last reply
    0
    • F fmardani

      Hi, I believe i understand the concept of delegates. To put it simply, I think they are used to invoke methods in another class. What I don't quite understand is: What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method. Can you please help to understand this issue? Thanks

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

      fmardani wrote:

      What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method.

      Because a delegate creates a link between classes where there often isn't one. For example, I create a form, form1. It has a modeless child, form2. I want to call a method in form1 when something happens in form2. Now, I CAN make a reference to form1 inside form2, but that's kind of ugly. Delegates are a nicer solution. Events can also be chained, so calling one event can call methods in several class instances. That's all happening for you, for free. Every time I go back to C++, delegates are the first thing I miss. Yes, I have function pointers, but it's a lot more work.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      Y 1 Reply Last reply
      0
      • C Christian Graus

        fmardani wrote:

        What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method.

        Because a delegate creates a link between classes where there often isn't one. For example, I create a form, form1. It has a modeless child, form2. I want to call a method in form1 when something happens in form2. Now, I CAN make a reference to form1 inside form2, but that's kind of ugly. Delegates are a nicer solution. Events can also be chained, so calling one event can call methods in several class instances. That's all happening for you, for free. Every time I go back to C++, delegates are the first thing I miss. Yes, I have function pointers, but it's a lot more work.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        Y Offline
        Y Offline
        yueue
        wrote on last edited by
        #3

        faint ~~~

        1 Reply Last reply
        0
        • F fmardani

          Hi, I believe i understand the concept of delegates. To put it simply, I think they are used to invoke methods in another class. What I don't quite understand is: What is the point of using delegates when you can easily call the method in the other class the normal way which is create an iinstance of the class and then call the method. Can you please help to understand this issue? Thanks

          N Offline
          N Offline
          NasimKaziS
          wrote on last edited by
          #4

          In oop its an usual practice for an object to send a message to another object, itq common for an object to report back to the sender, resulting in two way conversation between objects (callback methods). C, C++ did this using function pointers. C# does it using delegates. Its a special type of object which contains details about the method rather than the data. They are used for two purposes 1. Callback 2. Event handling i hope this helps ---------------------------------------------------------------

          where there is a will there is a way

          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