#1,191 – Lambda Can’t Capture ref or out Parameters

Lambda expressions can make use of variables declared in a containing scope, i.e. outside of the expression itself.  They cannot, however, use variables that are defined as ref or out parameters in an outer scope.

In the example below, it’s a compile-time error to include the valOut parameter in the lambda expression.

        static void SomeMethod(int valIn, out int valOut)
        {
            int local;

            Action doCalc = () =>
            {
                local = valIn * 2;   // this is ok
                valOut = valIn * i;  // this is not--compile-time error
            };
        }

As an alternative, you can assign a value returned by the lambda to an out parameter.

        static void SomeMethod(int valIn, out int valOut)
        {
            // Ok to assign result of lambda to
            // out parameter
            Func<int> doCalc2 = () => valIn * 2;
            valOut = doCalc2();   // Allowed
        }
Advertisements

#1,190 – A Lambda Expression Can Be Recursive

A lambda expression can be recursive.  That is, it can invoke the same delegate that the lambda is being assigned to.  As with any recursive method, you need to make sure that there is termination logic to prevent the recursion from continuing indefinitely.

Below is an example of a simple recursive lambda expression.

            // Must assign delegate so that we can
            // reference it in lambda
            Action<int> countdown = null;
                
            countdown = (i) =>
            {
                if (i > 0)
                { 
                    Console.WriteLine(i);
                    countdown(i-1);
                }
            };

            countdown(5);

1190-001

#1,189 – Lambda Expression Can Capture Static Data

A lambda expression set within a static method can make use of a class’ static data.  If the static data changes after the lambda is defined, invoking the lambda later will reflect the new (modified) static data.

Suppose that we have a Dog.SetStaticLambda method that defines a lambda using some static data:

    public class Dog
    {
        public string Name { get; set; }

        public Dog(string name)
        {
            Name = name;
        }

        public static string DogMotto;

        private static Action myStaticLambda;

        public static void SetStaticLambda()
        {
            myStaticLambda = () => Console.WriteLine("Motto: {0}", DogMotto);
        }

        public static void InvokeStaticLambda()
        {
            myStaticLambda();
        }
    }

Below, we make use of these methods. Note that if we invoke the lambda after changing the static data, the new data is used.

            Dog.DogMotto = "Serving mankind for generations";
            Dog.SetStaticLambda();
            Dog.InvokeStaticLambda();  // Serving..

            Dog.DogMotto = "We want more treats";
            Dog.InvokeStaticLambda();  // Want more..

1189-001

#1,188 – Lambda Expression Can Capture Instance Data

A lambda expression set within an instance method can make use of instance data within the object.  If the instance data changes after the lambda is defined, invoking the lambda later will reflect the new (modified) instance data.

Suppose that we have a Dog.SetLambda method that defines a lambda using some instance data:

    public class Dog
    {
        public string Name { get; set; }

        public Dog(string name)
        {
            Name = name;
        }

        private Action myLambda;

        public void SetLambda()
        {
            myLambda = () => Console.WriteLine("Dog is {0}", Name);
        }

        public void InvokeLambda()
        {
            myLambda();
        }
    }

Below, we make use of these methods. Note that if we invoke the lambda after changing the instance data, the new data is used.

            Dog d = new Dog("Bowser");
            d.SetLambda();
            d.InvokeLambda();  // Bowser

            d.Name = "Lassie";
            d.InvokeLambda();  // Lassie

1188-001

#1,187 – Using a Lambda Expression When Adding to an Invocation List

Recall that an instance of a delegate can refer to more than one method (delegates are multicast).  You can use anonymous methods to declare a new instance of a delegate or to add methods to a delegate’s invocation list.

Since lambda expressions supersede anonymous methods, you can also use lambdas to initialize a delegate or to add to its invocation list.

        private delegate void StringHandlerDelegate(string s);

        static void Main(string[] args)
        {
            // Declare new delegate instance, with lambda
            // as its invocation list
            StringHandlerDelegate del1 = 
                (s) => Console.WriteLine(s);

            // Now add a 2nd method to delegate, also using
            // lambda expression
            del1 +=
                (s) => Console.WriteLine(new string(s.Reverse().ToArray())); 

            // When we invoke the delegate, both lambdas
            // are invoked
            del1("Mairzy Doats and Dozy Doats");

            Console.ReadLine();
        }

1187-001

#1,186 – Capturing a foreach Iteration Variable in a Lambda Expression

If you capture a for loop iteration variable in a lambda expression, the value of the variable that the expression uses will be whatever the final value of the iteration variable is when the loop completes.

Capturing the iteration variable in a foreach loop works differently.  When you capture the iteration variable in a foreach loop, the lambda expression has a copy of the iteration variable with the value that it had at the time that it was captured.

            int[] primes = { 2, 3, 5, 7, 11, 13 };
            List<Action> dels = new List<Action>();

            // Capture foreach iteration variable
            foreach (int p in primes)
                dels.Add(() => Console.WriteLine(p));

            foreach (Action a in dels)
                a();

1186-001

#1,183 – How to Correctly Capture a for Loop Variable in a Lambda Expression

If you directly capture a for loop variable within a lambda expression, the value used when executing the expression will typically be the value of the variable at the time that the loop exits.  If you instead want to capture a variable whose value is the value of the for loop variable while the loop is executing, you can use a local variable.

            Action[] dels = new Action[3];

            // How to correctly capture for loop variable
            for (int i = 0; i < 3; i++)
            {
                int iLocal = i;
                dels[i] = () => Console.WriteLine(iLocal + 1);
            }

            // Prints 1/2/3
            foreach (Action d in dels)
                d();

1183-001