Lambda Arguments

Intertech Tutorials

Lambdas with Multiple Arguments/No Arguments

Currently, your lambda expression processes a single input argument against a single code statement. However, a lambda expression can take multiple input arguments if the underlying delegate requires them.

Consider a custom delegate named BinaryOp. This delegate can invoke any method that takes two integers as input and returns an integer. You could use the following C# lambda to interact with the BinaryOp delegate behind the scenes. Multiple arguments must be wrapped in parentheses.


// C#
namespace LambdaExpressionsMultipleParams
{
// This delegate can point to any method that
// processes two integers and returns an integer.
public delegate int BinaryOp(int x, int y);

class Program
{
static void Main(string[] args)
{
  Console.WriteLine("***** Lambdas with Multiple Params! *****\n");

  // Register with the delegate as a lambda expression.
  BinaryOp b = new BinaryOp((x, y) => x + y);

  // This will execute the lambda.
  Console.WriteLine(b(10, 10));
  Console.ReadLine();
}
}
}

The VB code is similar:


' VB
Option Strict On

' This delegate can point to any method that
' processes two integers and returns an integer.
Public Delegate Function BinaryOp(ByVal x As Integer, _
ByVal y As Integer) As Integer

Module Program
Sub Main()
Console.WriteLine("***** Lambdas with Multiple Params! *****" + vbLf)

' Register with the delegate as a lambda expression.
Dim b As New BinaryOp(Function(x, y) x + y)

' This will execute the lambda expression.
Console.WriteLine(b(10, 10))

Console.ReadLine()
End Sub
End Module

If you are using a lambda expression to interact with a delegate taking no arguments, simply use an empty set of parentheses to represent ‘no params’. Note that, in this case, you will need to call the Invoke() method of the delegate object manually.


// C#
namespace LambdaExpressionsNoParams
{
// This delegate can point to any method that
// take no args and returns an string.
public delegate string GetTime();

class Program
{
static void Main(string[] args)
{
  Console.WriteLine("***** Lambdas with No Params! *****\n");

  // Register with the delegate as a lambda expression.
  GetTime time = new GetTime(() => DateTime.Now.ToString());

  // This will execute the lambda.
  Console.WriteLine(time.Invoke());
  Console.ReadLine();
}
}
}

Here is the corresponding VB code:


' VB
Option Strict On

' This delegate can point to any method that
' take no args and returns an string.
Public Delegate Function GetTime() As String

Module Program
Sub Main()
Console.WriteLine("***** Lambdas with No Params! *****" + vbLf)

' Register with the delegate as a lambda expression.
Dim t As New GetTime(Function() DateTime.Now.ToString())

' This will execute the lambda expression.
Console.WriteLine(t.Invoke())

Console.ReadLine()
End Sub
End Module

Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.