Menu

Understand lambda expressions

asy proger

This is a simplistic explanation. The net is full of "C# lambda expression" examples.

In C# you can enter a lambda expression at places where the compiler expects a delegate.
Often the delegate has one parameter and the compiler always knows its type and the return type.
To reduce syntax to the minimum, you only have to write the parameter name, the lambda operator and the code to execute.
The lambda 'operator' is "=>". Read it as "... goes into ..."

Example 1:

N => do something with int N



Example 2:

N => 
{
  bool x;     
  do something with int N;
  do some more with x;
  return x;
}



These lambda expressions match delegate definitions as follows:

delegate void Action1 (int num);
and
delegate bool Action2 (int num);



The compiler generates an anonymous method (name unknown to the programmer) for each lambda expression.

Lambda expressions may access local variables of the method they are called from.
See C# language specification version 3.0, chapter 7.14.4, Outer variables.

For each initialization of an outer variable, an instance is created (probably on the GC heap) and a reference to this outer variable is passed into the lambda expression for later use.
In the example below, 5 instances of 'requestNum' are created and passed into the 5 instances of lambda expression, each waiting for its invocation.
Be aware, we could not have used "i" as an outer variable. i is initialized (=instantiated) only once!

void Example ( ActorOutput output, WcfMessage msg )
{
  for (int i=0; i<5; i++)
  {
    int requestNum = i;
    output.SendOut (msg, rsp => {Console.WriteLine ("Response to #" + requestNum
                                 +" = "+rsp.Message.ToString(); 
                                 return null;
                                });
  }
}


Handling responses to ActorOutput.SendOut() with lambda expressions

The ActorOutput.SendOut() method has an optional second parameter accepting a lambda expression of delegate type "WcfReqIdent AsyncResponseHandler (WcfReqIdent rsp);"
This delegate will be executed when the response to the locally sent request arrives.
The response handler must return null when the message is handled, otherwise it has to return the message, that will be passed to the default "WcfMessageHandlerDelegate" (set in the constructor).

The asynchronous response to the Send operation will be executed after Example() has returned (see above).
This is guaranteed, as the same thread that sent the message, has to fetch the response from the message queue and execute the lambda expression.

[Implement a client using AsyncWcfLib] shows an example using extension method 'On<T>' and lambda expressions to downcast the received message to its concrete type and dispatch it to the matching messagehandler. See also [Understand extension methods].


Related

Wiki: Home
Wiki: Implement a client using AsyncWcfLib
Wiki: Understand extension methods

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.