Latest web development tutorials

C # delegate (Delegate)

C # Principal (Delegate) similar to C or C ++ function pointer.Principal (Delegate) there is a method for a reference type variable references.Reference can be changed at runtime.

Principal (Delegate) specifically intended for events and callback methods. All delegate (Delegate) are derived fromSystem.Delegate class.

Statement commission (Delegate)

Proxy statement determines the method may be referenced by the delegate. Delegate can point to a method having the same label.

For example, suppose you have a delegate:

public delegate int MyDelegate (string s);

The above commission may be used to refer to any method that takes a singlestringparameter and returns aninttype variable.

The syntax for declaring commissioned as follows:

delegate <return type> <delegate-name> <parameter list>

Examples of the commission (Delegate)

Once declared a delegate type, delegate object must use thenew keyword to create, and with a particular methods.When you create a delegate parameter passed to thenew statement as a method call, like writing, but no arguments.E.g:

public delegate void printString (string s);
...
printString ps1 = new printString (WriteToScreen);
printString ps2 = new printString (WriteToFile);

The following example demonstrates the commission's statement, instantiate and use, the delegate can be used to reference method with an integer argument and returns an integer value.

using System;

delegate int NumberChanger (int n);
namespace DelegateAppl
{
   class TestDelegate
   {
      static int num = 10;
      public static int AddNum (int p)
      {
         num + = p;
         return num;
      }

      public static int MultNum (int q)
      {
         num * = q;
         return num;
      }
      public static int getNum ()
      {
         return num;
      }

      static void Main (string [] args)
      {
         // Create a delegate instance NumberChanger nc1 = new NumberChanger (AddNum);
         NumberChanger nc2 = new NumberChanger (MultNum);
         // Call the method using a delegate object nc1 (25);
         Console.WriteLine ( "Value of Num: {0}", getNum ());
         nc2 (5);
         Console.WriteLine ( "Value of Num: {0}", getNum ());
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Value of Num: 35
Value of Num: 175

Commissioned multicast (Multicasting of a Delegate)

Delegate object can use the "+" operator to merge. A delegate to call it merge merge two delegates. Only the same type of trust can be combined. "-" Operator can be used to remove the component from the combined delegate delegate.

Use this useful feature commissioned, you can create a list of delegate calls the method when it is called to invoke. This is called delegatedmulticast (multicasting), also known as multicasting.The following program demonstrates a multicast delegate:

using System;

delegate int NumberChanger (int n);
namespace DelegateAppl
{
   class TestDelegate
   {
      static int num = 10;
      public static int AddNum (int p)
      {
         num + = p;
         return num;
      }

      public static int MultNum (int q)
      {
         num * = q;
         return num;
      }
      public static int getNum ()
      {
         return num;
      }

      static void Main (string [] args)
      {
         // Create a delegate instance NumberChanger nc;
         NumberChanger nc1 = new NumberChanger (AddNum);
         NumberChanger nc2 = new NumberChanger (MultNum);
         nc = nc1;
         nc + = nc2;
         // Call multicast nc (5);
         Console.WriteLine ( "Value of Num: {0}", getNum ());
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Value of Num: 75

Principal (Delegate) uses

The following example demonstrates the usage of delegation. PrincipalprintStringbe used to reference method with a string as input and does not return anything.

We use this delegate to call the two methods, the first string printed to the console, and the second string to print to a file:

using System;
using System.IO;

namespace DelegateAppl
{
   class PrintString
   {
      static FileStream fs;
      static StreamWriter sw;
      // Proxy statement public delegate void printString (string s);

      // This method prints to the console public static void WriteToScreen (string str)
      {
         Console.WriteLine ( "The String is: {0}", str);
      }
      // This method prints to file public static void WriteToFile (string s)
      {
         fs = new FileStream ( "c: \\ message.txt",
         FileMode.Append, FileAccess.Write);
         sw = new StreamWriter (fs);
         sw.WriteLine (s);
         sw.Flush ();
         sw.Close ();
         fs.Close ();
      }
      // This method the delegate as a parameter, and use it to call the method public static void sendString (printString ps)
      {
         ps ( "Hello World");
      }
      static void Main (string [] args)
      {
         printString ps1 = new printString (WriteToScreen);
         printString ps2 = new printString (WriteToFile);
         sendString (ps1);
         sendString (ps2);
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

The String is: Hello World