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!

Filed under: , , , , ,

Comments

# Lambda Expressions: An Introduction

Sunday, October 11, 2009 6:57 PM by Deborah's Developer MindScape

My presentation at our local Code Camp was fun. I covered a lot of material and had a GREAT audience

# Lambda Expressions: Execution

Friday, October 16, 2009 3:26 PM by Deborah's Developer MindScape

Lambda expressions can be assigned to a delegate variable. The lambda expression is then executed when

Leave a Comment

(required) 
(required) 
(optional)
(required)