You can put multiple statements in a lambda's { } and declare variables in them - CSharp Custom Type

CSharp examples for Custom Type:Lambda

Description

You can put multiple statements in a lambda's { } and declare variables in them

Demo Code



using System;//from  w w  w .  java  2  s.c  o  m
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int[] numArray = numbers.ToArray();
        List<string> words = new List<string> { "one", "two", "three", "four", "five" };

        string str = "numbers.ForEach(n => { var s = n.ToString(); Console.WriteLine(s); } );";
        Console.WriteLine(str);
        numbers.ForEach(n => { string s = n.ToString(); Console.WriteLine(s); });
    }

    static void PrintThis(string message)
    {
        Console.WriteLine(message);
    }

}

Result


Related Tutorials