Action delegate in C# 2.0

I have used in the past predicate delegate, which is used when you want to test for a certain condition. However Action delegate is used when you want to perform certain action on specified value. The specified value could be complex type or value type. It could be printing to console window or modifying the current object or value. Here is simple example in which I am making use of Action<T> delegate.

image

In the example above, I have a collection of persons that I want to write to the console window. I create an action delegate which takes Person object and prints its name property to the console. In order to apply the delegate on each person object in the person collection, I make use of the foreach method passing in the action delegate.

No Comments