1.23. Create and Use Lambda Expressions

Problem

You need to use an inline function, which is a single-line function that does not require a standard function code block, in an effort to save some development time or support LINQ (discussed in more detail in Chapter 6).

Solution

Create a lambda expression using the Function keyword, and use it directly or pass it as an argument to a function that requires a delegate.

How It Works

To use a simple function, you typically start by creating the function. The following example takes an Integer and multiplies it by itself:

Private Shared Function Square(ByVal num As Integer) As Integer
    Return num * num
End Function

If you need to pass a function as an argument to some method, you could use a delegate ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.