T B H P N

C# Lambdas

Synopsis:

A C# lambda is a locally generated anonymous delegate that can capture data it needs and transport it to a new location where it is eventually exectured. Lambdas can be passed to, and returned from, other functions.
Delegates can bind to any lambda with the same signature, e.g., parameter sequence and return value.
Here is demonstration code that uses a lambda in a typical way, including use of capture data.

Code in File Lambdas.cs

  /////////////////////////////////////////////////////////////////////
  // Lambdas.cs - demonstrate basic creation and use of lambdas      //
  //                                                                 //
  // Jim Fawcett, CSE681 - Software Modeling and Analysis, Fall 2017 //
  /////////////////////////////////////////////////////////////////////

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  namespace Lambdas
  {
    ///////////////////////////////////////////////////////////////////
    // LambdaCaptureDemo class
    //   - used to generate lambda's captured data

    public class LambdaCaptureDemo
    {
      public LambdaCaptureDemo(string thisClass)
      {
        thisClass_ = thisClass;
      }
      public string myClass() { return thisClass_; }
      public string name { get; set; } = "no name";
      private string thisClass_ = "";
    }
    ///////////////////////////////////////////////////////////////////
    // MakeLambda class
    //   - creates lambdas - surprise!

    public class MakeLambda
    {
      public static Action doIt(string myClass)
      {
        LambdaCaptureDemo capture = new LambdaCaptureDemo(myClass);
        capture.name = "Jim Fawcett";

        // - a lambda is defined and bound to Action delegate
        // - any type of instance used by the lambda is captured by the lambda.
        //   That is, the lamda acquires a reference to the instance and can
        //   use any time before it is finalized. capture.name and return value of
        //   capture.myClass() are both captured by the lambda, and travel with it    
        //   wherever it goes.

        Action act = () =>
        {
          Console.Write("\n  {0}'s class is {1}", capture.name, capture.myClass());
        };
        return act;
      }
    }
    ///////////////////////////////////////////////////////////////////
    // LambdasDemo class
    //
    // - Here's what happens:
    //   - MakeLambda.doIt method creates a lambda, with the help of
    //     LambdaCaptureDemo class in it's static method doIt.
    //   - A reference to an Action delegate bound to the lambda is
    //     transported out of the scope of the doIt method to the caller,
    //     e.g., LambdaDemo.Main.
    //   - The lambda is executed as soon as it is returned - that's what
    //     the trailing Invoke() does.

    class LambdasDemo
    {
      static void Main(string[] args)
      {
        Console.Write("\n  Demonstrate creation and use of Lambda");
        Console.Write("\n ========================================");

        MakeLambda.doIt("CSE681 - SMA").Invoke();
        Console.Write("\n\n");
      }
    }
  }

Lambdas Output:

  
  Demonstrate creation and use of Lambda
 ========================================
  Jim Fawcett's class is CSE681 - SMA

Press any key to continue . . .

Example Code:


  1. Generics are created at run-time

CST strip