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.
- static void Display(string str)
- {
- Console.WriteLine(str);
- }
- static void Main(string[] args)
- {
- Action<string> ObjActionDel = new Action<string>(Display);
- List<string> names = new List<string>() { "India","Russia","US","Australia","New Zealand" };
- names.ForEach(x => Console.WriteLine(x));
- Console.ReadLine();
- }
OutputIndia
Russia
US
Australia
New Zealand