Lambda Expressions: Action Delegates
Posted
Sun, Oct 11 2009 16:04
by
Deborah Kurata
Just as the name implies, an action delegate encapsulates a method that performs an action and has no return value. It takes up to four parameters (and this number is increased in .NET 4.0).
[To begin with an overview of lambda expressions, start here.]
The ForEach method of the List<T> is an example of an action delegate. It takes an action delegate as a parameter.
One of the common uses of ForEach is to display all of the elements in a list using one line of code.
In C#:
custList.ForEach(c =>
Debug.WriteLine(c.LastName));
In VB:
custList.ForEach(AddressOf WriteToDebug)
NOTE: VB lambda expressions do not currently support action delegates. In the above example, the code uses a named method as the action delegate. VB 10 lambda expressions will support action delegates using the Sub keyword instead of the Function keyword.
Enjoy!