Multicast delegate example - CSharp Custom Type

CSharp examples for Custom Type:delegate

Description

Multicast delegate example

Demo Code

using System;//from  ww w  . j  av a2  s. co  m
public delegate void ProgressReporter (int percentComplete);
public class Util
{
   public static void HardWork (ProgressReporter p)
   {
      for (int i = 0; i < 10; i++)
      {
         p (i * 10);                           // Invoke delegate
         System.Threading.Thread.Sleep (100);  // Simulate hard work
      }
   }
}
class Test
{
   static void Main()
   {
      ProgressReporter p = WriteProgressToConsole;
      p += WriteProgressToFile;
      Util.HardWork (p);
   }
   static void WriteProgressToConsole (int percentComplete) => Console.WriteLine (percentComplete);
   static void WriteProgressToFile (int percentComplete) => System.IO.File.WriteAllText ("progress.txt", percentComplete.ToString());
}

Result


Related Tutorials