uses the invocation list to calculate a factorial : Delegate : Delegate C# Examples


C# Examples » Delegate » Delegate »

 

uses the invocation list to calculate a factorial









    
using  System;

public  delegate  int  IncrementDelegate(ref  short  refCount);

public  class  Factorial  {
        public  static  void  Main()  {

                IncrementDelegate[]  values  =  {  Incrementer,  Incrementer,Incrementer,  Incrementer,  Incrementer};
                IncrementDelegate  del  =  (IncrementDelegate)
                IncrementDelegate.Combine(values);
                long  result  =  1;
                short  count  =  1;
                foreach  (IncrementDelegate  number  in  del.GetInvocationList())  {
                        result  =  result  *  number(ref  count);
                }
                Console.WriteLine("{0}  factorial  is  {1}",  del.GetInvocationList().Length,  result);
        }

        public  static  int  Incrementer(ref  short  refCount)  {
                return  refCount++;
        }

}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Delegate