Delegates can refer to instance methods : Delegate : Delegate C# Examples


C# Examples » Delegate » Delegate »

 

Delegates can refer to instance methods









    
using  System;  
  
delegate  string  StrMod(string  str);  
  
class  StringOps  {  
    public  static  string  replaceSpaces(string  a)  {  
        Console.WriteLine("replaceSpaces");  
        return  a;  
    }    
  
    public  static  string  removeSpaces(string  a)  {  
        Console.WriteLine("removeSpaces");  
        return  a;  
    }    
  
    public  static  string  reverse(string  a)  {  
        Console.WriteLine("reverseSpaces");  
        return  a;  
    }  
}  
  
class  MainClass  {      
    public  static  void  Main()  {    
  
        //  Initialize  a  delegate.  
        StrMod  strOp  =  new  StrMod(StringOps.replaceSpaces);  
        string  str;  
  
        //  Call  methods  through  delegates.  
        str  =  strOp("This  is  a  test.");  
            
        strOp  =  new  StrMod(StringOps.removeSpaces);  
        str  =  strOp("This  is  a  test.");  
  
        strOp  =  new  StrMod(StringOps.reverse);  
        str  =  strOp("This  is  a  test.");  

    }  
}
    
   
  
   



Output

replaceSpaces
removeSpaces
reverseSpaces


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Delegate