Demonstrate a very simple delegate callback. - CSharp Custom Type

CSharp examples for Custom Type:delegate

Description

Demonstrate a very simple delegate callback.

Demo Code

using System;//from  w w w.  j a  v  a  2s  . c o m
class Program
{
   delegate int MyDelType(string name); // ... or here, nested in the class.
   static void Main(string[] args)
   {
      MyDelType del = new MyDelType(Program.CallBackMethod);
      UseTheDel(del, "hello");
   }
   private static void UseTheDel(MyDelType del, string arg)
   {
      if (del == null) return; // Don't invoke a null delegate!
         Console.WriteLine("UseTheDel writes {0}", del(arg));
      }
      public static int CallBackMethod(string stringPassed)
      {
         Console.WriteLine("CallBackMethod writes: {0}", stringPassed);
         return stringPassed.Length;  // Delegate requires an int return.
      }
}

Result


Related Tutorials