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. When do we have to use delegate in C#?

When do we have to use delegate in C#?

Scheduled Pinned Locked Moved C#
csharpquestionlearning
7 Posts 5 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.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hi there. I'm new on C# :sigh: . I want to know when we have to use delegate in C#.:confused: Of course except Eevents. Thanks in advance.

    Sorry for my English. I'm a freshman .

    M Z 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hi there. I'm new on C# :sigh: . I want to know when we have to use delegate in C#.:confused: Of course except Eevents. Thanks in advance.

      Sorry for my English. I'm a freshman .

      M Offline
      M Offline
      MarkB777
      wrote on last edited by
      #2

      You can use delegates for heaps of things, they are just function pointers. I used them quite often when multithreading. Events are where they seem to be most commonly used though.

      Mark Brock Click here to view my blog

      M 1 Reply Last reply
      0
      • M Mohammad Dayyan

        Hi there. I'm new on C# :sigh: . I want to know when we have to use delegate in C#.:confused: Of course except Eevents. Thanks in advance.

        Sorry for my English. I'm a freshman .

        Z Offline
        Z Offline
        Zoltan Balazs
        wrote on last edited by
        #3

        Delegates on msdn[^]

        Work @ Network integrated solutions | Flickr | A practical use of the MVC pattern citizen 340340

        A 1 Reply Last reply
        0
        • M MarkB777

          You can use delegates for heaps of things, they are just function pointers. I used them quite often when multithreading. Events are where they seem to be most commonly used though.

          Mark Brock Click here to view my blog

          M Offline
          M Offline
          Mohammad Dayyan
          wrote on last edited by
          #4

          Thanks Mark.

          Sorry for my English. I'm a freshman .

          1 Reply Last reply
          0
          • Z Zoltan Balazs

            Delegates on msdn[^]

            Work @ Network integrated solutions | Flickr | A practical use of the MVC pattern citizen 340340

            A Offline
            A Offline
            Abdul Gafoor
            wrote on last edited by
            #5
            1. u can use a delegate if u want an object to have several method with same signature (ie, a family of Methods) but with different algorithm and u can select between these method dynamically using a delegate. Something like... delegate double BinaryOperation(double a, double b) BinaryOperation binaryOperation; private double Add(double a, double b) { return a + b; } private double divide(double a, double b) { return a / b; } public static void BinaryOperationsButton_click(sender object, EventArgs e) { if(object is btnAdd) binaryOperation = new BinaryOperation(Add); else if(object is btnDivide) binaryOperation = new BinaryOperation(Divide); binayOperation.BeginInvoke(operand1,operand2,null,null); }
            R 1 Reply Last reply
            0
            • A Abdul Gafoor
              1. u can use a delegate if u want an object to have several method with same signature (ie, a family of Methods) but with different algorithm and u can select between these method dynamically using a delegate. Something like... delegate double BinaryOperation(double a, double b) BinaryOperation binaryOperation; private double Add(double a, double b) { return a + b; } private double divide(double a, double b) { return a / b; } public static void BinaryOperationsButton_click(sender object, EventArgs e) { if(object is btnAdd) binaryOperation = new BinaryOperation(Add); else if(object is btnDivide) binaryOperation = new BinaryOperation(Divide); binayOperation.BeginInvoke(operand1,operand2,null,null); }
              R Offline
              R Offline
              RobScripta
              wrote on last edited by
              #6

              I've seen similar examples but I still can't see the point. What's wrong with this code: { if (sender.name == "btnAdd") { double result = Add(operand1, operand2); } if (sender.name == "btnDivide") { double result = divide(operand1, operand2); } } for me makes reading and understanding simpler. So, I am not using delegates, and worrying i'm missing something ;) Rob

              A 1 Reply Last reply
              0
              • R RobScripta

                I've seen similar examples but I still can't see the point. What's wrong with this code: { if (sender.name == "btnAdd") { double result = Add(operand1, operand2); } if (sender.name == "btnDivide") { double result = divide(operand1, operand2); } } for me makes reading and understanding simpler. So, I am not using delegates, and worrying i'm missing something ;) Rob

                A Offline
                A Offline
                Abdul Gafoor
                wrote on last edited by
                #7

                :) the previous example did not do its job even though it show the delegates asynchronous ability. Something assignable gives you the flexibility and scalability. just look at this situation; delegate void CipherInvoker(byte[] array); CipherInvoker cipher; public void ChooseCipher() { if(option == "RSACipher") cipher = new CipherInvoker(RSACipher); else if(option == "DESCipher") cipher = new CipherInvoker(DESCipher); CipherFile("blah", cipher); } public void CipherFile(string filname, CipherInvoker cipher) { // blah blah cipher(); //blah blah } lets assume that , you found out new efficient and secure way of ciphering or u want to implement ciphering with a another alogorithm; then u can implement it and u can use it without altering unnecessary places u may change like public void ChooseCipher() { if(option == "RSACipher") cipher = new CipherInvoker(RSACipher); else if(option == "DESCipher") cipher = new CipherInvoker(DESCipher); else if(option == "MyCipher") cipher = new CipherInvoker(MyCipher); CipherFile("blah" ,cipher); }

                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