Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in H:\root\home\sklibraries-001\www\rajmhr\wp-content\plugins\wp-markdown\markdownify\markdownify.php on line 299
Callback Delegate – RajTech

Callback Delegate

Simply, delegates are the type (reference type) in c#, similar like pointers in c and c++, that holds the method reference. This feature provides the type safe function pointer. It is also used when you have to call method which is in separate thread running in the system. The basis use and purpose of delegate is “Call Back Function”. Let’s check this feature.

Callback: Callback generally means a executable code that is passed as an argument to some other code.

class Program
   {
       static void Main(string[] args)
       {
           Program pgm = new Program();
           var data = new Data();
           data.LoadData(pgm.Progress);
           Console.ReadLine();
       }

       public void Progress(int x)
       {
           Console.WriteLine(String.Format("{0} % completed", x * 10));
           if (x == 10)
           {
               Console.WriteLine("Done !!");
           }
       }
   }

   public class Data
   {
       public delegate void UpdateDelegate(int x1);
       public void LoadData(UpdateDelegate upDelegate)
       {
           for (int i = 0; i <= 10; i++)
           {
               Thread.Sleep(1000);
               upDelegate.Invoke(i);
           }
       }
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *