Action Delegate using C#

This is a readymade available delegate. Action delegate also provides a method invoke to call the method.

In this demo we have created a static method Display which accepts a string input parameter. We would pass this method name to our delegate. A List of type string is defined. And Foreach method is used to iterate through the collection.

  1. static void Display(string str)    
  2. {    
  3.     Console.WriteLine(str);    
  4. }    
  5. static void Main(string[] args)    
  6. {    
  7.     Action<string> ObjActionDel = new Action<string>(Display);    
  8.     List<string> names = new List<string>() { "India","Russia","US","Australia","New Zealand" };    
  9.     names.ForEach(x => Console.WriteLine(x));    
  10.     Console.ReadLine();    
  11. }    
Output

India
Russia
US
Australia
New Zealand