Add both static and non-static function to a delegate : Delegate : Delegate C# Examples


C# Examples » Delegate » Delegate »

 

Add both static and non-static function to a delegate









    
using  System;


delegate  void  FunctionToCall();

class  MyClass
{
      public  void  nonStaticMethod()
      {
            Console.WriteLine("nonStaticMethod");
      }

      public  static  void  staticMethod()
      {
            Console.WriteLine("staticMethod");
      }
}

class  MainClass
{
      static  void  Main()
      {
            MyClass  t  =  new  MyClass();        
            FunctionToCall  functionDelegate;
            functionDelegate  =  t.nonStaticMethod;                      

            functionDelegate  +=  MyClass.staticMethod;
            functionDelegate  +=  t.nonStaticMethod;
            functionDelegate  +=  MyClass.staticMethod;

            functionDelegate();
      }
}
    
   
  
   



Output

nonStaticMethod
staticMethod
nonStaticMethod
staticMethod


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Delegate