tags:

views:

281

answers:

4

I've used delegates when designing win forms in .NET... i.e. drag/drop a button, double click, and fill in the myButton_click event. I want to understand how to create and use user-defined delegates in C#.

How are user-defined delegates used and created in C# ?

+8  A: 

I suggest reading a tutorial on the topic.

Basically, you declare a delegate type:

public delegate void MyDelegate(string message);

Then you can either assign and call it directly:

MyDelegate = SomeFunction;
MyDelegate("Hello, bunny");

Or you create an event:

public event MyDelegate MyEvent;

Then you can add an event handler from outside like this:

SomeObject.MyEvent += SomeFunction;

Visual Studio is helpful with this. After you entered the +=, just press tab-tab and it will create the handler for you.

Then you can fire the event from inside the object:

if (MyEvent != null) {
    MyEvent("Hello, bunny");
}

That's the basic usage.

OregonGhost
+1, you just forgot the "delegate" keyword on first code line.
ybo
Oops. Thanks for pointing out.
OregonGhost
+1  A: 
public delegate void testDelegate(string s, int i);

private void callDelegate()
{
    testDelegate td = new testDelegate(Test);

    td.Invoke("my text", 1);
}

private void Test(string s, int i)
{
    Console.WriteLine(s);
    Console.WriteLine(i.ToString());
}
OneSHOT
+1  A: 

Not quite a duplicate (can't find a duplicate) but lots of information here on SO, try

Differnce between Events and Delegates to get started, then look at

When to use . . .

What are Closures

Whis is this delegate doing . . .

Hope these help

Binary Worrier
+1  A: 

For broad answer check this article by mohamad halabi. For shorter answer check this slightly modified example from the c:/Program Files/Microsoft Visual Studio 9.0/Samples/1033/ folder ...

using System;
using System.IO; 


namespace DelegateExample
{
  class Program
  {
    public delegate void PrintDelegate ( string s );

    public static void Main ()
    {
      PrintDelegate delFileWriter = new PrintDelegate ( PrintFoFile );
      PrintDelegate delConsoleWriter = new PrintDelegate ( PrintToConsole);
      Console.WriteLine ( "PRINT FIRST TO FILE by passing the print delegate -- DisplayMethod ( delFileWriter )" );

      DisplayMethod ( delFileWriter );      //prints to file
      Console.WriteLine ( "PRINT SECOND TO CONSOLE by passing the print delegate -- DisplayMethod ( delConsoleWriter )" );
      DisplayMethod ( delConsoleWriter ); //prints to the console
      Console.WriteLine ( "Press enter to exit" );
      Console.ReadLine ();

    }

    static void PrintFoFile ( string s )
    {
      StreamWriter objStreamWriter = File.CreateText( AppDomain.CurrentDomain.BaseDirectory.ToString() + "file.txt" );
      objStreamWriter.WriteLine ( s );
      objStreamWriter.Flush ();
      objStreamWriter.Close ();
    }


    public static void DisplayMethod ( PrintDelegate delPrintingMethod )
    { 
      delPrintingMethod( "The stuff to print regardless of where it will go to" ) ;
    }

    static void PrintToConsole ( string s )
    {
      Console.WriteLine ( s );    
    } //eof method 
  } //eof classs 
} //eof namespace
YordanGeorgiev