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#
tutorialquestion
3 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.
  • D Offline
    D Offline
    deanoA
    wrote on last edited by
    #1

    Can anybody show me the shortest example on using a delegate? Im just trying to gather ideas on how to program in "keep it short and simple way" :) "To teach is to learn twice"

    B 2 Replies Last reply
    0
    • D deanoA

      Can anybody show me the shortest example on using a delegate? Im just trying to gather ideas on how to program in "keep it short and simple way" :) "To teach is to learn twice"

      B Offline
      B Offline
      Blake Coverett
      wrote on last edited by
      #2

      The shortest example? Well I guess that would be...

      class T{delegate void D();static void Main(){D d = new D(Main);d();}}

      ...but watch out for the stack overflow. -- -Blake (com/bcdev/blake)

      1 Reply Last reply
      0
      • D deanoA

        Can anybody show me the shortest example on using a delegate? Im just trying to gather ideas on how to program in "keep it short and simple way" :) "To teach is to learn twice"

        B Offline
        B Offline
        Blake Coverett
        wrote on last edited by
        #3

        The shortest example? Well I guess that would be...

        class T{delegate void D();static void Main(){new D(Main)();}}

        ...but watch out for the stack overflow. I suppose you'd like a useful example?

        class Example {
        delegate void StatusOutput(string text);

        static void Main() {
        	DoSomeWork(new StatusOutput(System.Console.WriteLine));
        	DoSomeWork(new StatusOutput(Alert));
        }
        
        static void Alert(string text) {
        	System.Windows.Forms.MessageBox.Show(text, "Status Update");
        }
        
        static void DoSomeWork(StatusOutput output) {
        	output("Working...");
        	System.Threading.Thread.Sleep(2000);
        	output("Done");
        }
        

        }

        That was about the shortest meaningful use I could think of. A delegate is an object that wraps a method. You can then pass this object around and use it later to call the method. There are lots of other more advanced uses, like invoking them asynchronously, but that's a start. -- -Blake (com/bcdev/blake)

        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