Lambda Expressions: Func Delegates
Posted
Sun, Oct 11 2009 16:49
by
Deborah Kurata
A Func delegate encapsulates a method that returns a value. It takes up to four parameters (and this number is increased in .NET 4.0) plus the return value.
[To begin with an overview of lambda expressions, start here.]
The following signature is a Func delegate that takes two integer parameters and returns a string.
In C#:
Func<int, int, string>
In VB:
Func(Of Integer, Integer, String)
There are many examples of Func delegates.
The following code demonstrates the Sum function that sums the sales total for all customers in the list (and assumes that a Customer object has a SalesTotal property).
In C#:
var total = custList.Sum(c =>
c.SalesTotal);
In VB:
Dim total = custList.Sum(Function(c) _
c.SalesTotal)
Enjoy!