Demonstrate a captured variable. : Anonymous delegate : Delegate C# Examples


C# Examples » Delegate » Anonymous delegate »

 

Demonstrate a captured variable.









    
using  System;    
    
delegate  int  CountIt(int  end);
    
class  MainClass  {    
  
    static  CountIt  counter()  {  
        CountIt  ctObj  =  delegate  (int  end)  {  
            Console.WriteLine("end:"+end);
            return  end;  
        };  
        return  ctObj;  
    }  
  
    public  static  void  Main()  {      
        CountIt  count  =  counter();  
  
        int  result  =  count(3);  
        result  =  count(5);    
    }  
}
    
   
  
   



Output

end:3
end:5


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Anonymous delegate