Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I spend my time on lamda expression and play around with this concept.
I am amazed that lamda expression (greet) and lamda expression(greet2) can be used inside a method such as main(), also used as a method inside class level.

is my understanding correct? I think I missed the power of Lamda expression.

class Program
        static void Main(string[] args)
            System.Linq.Expressions.Expression<Func<int, int>> e = x => x * x;

            int[] numbers = { 2, 3, 4, 5 };
            var squaredNumbers = numbers.Select(x => x * x);
            Console.WriteLine(string.Join(" ", squaredNumbers));

            Action<string> greet = name =>
                string greeting = $"Hello {name}!";

            greet2("Hello World!");


        static Action<string> greet2 = name =>
            string greeting = $"Hello {name}!";

What I have tried:

I tried this console program and validated my understanding. just feel amazed by this concept :)
Updated 26-Nov-22 16:58pm
PIEBALDconsult 28-Nov-22 9:56am    
Maybe post this as a Tip ?

1 solution

Yes, a lambda expression is used to create an anonymous function.

As you have already discovered, you specify input parameters (if any) on the left side of the lambda operator and an expression or a statement block on the other side, to create a lambda expression.

It can be of two forms:
1. Expression lambda that has an expression as its body:
(input-parameters) => expression
2. Statement lambda that has a statement block as its body:
(input-parameters) => { <sequence-of-statements> }
The body of an expression lambda can consist of a method call.

It's much easier:
- to create lambda expressions and statements that incorporate asynchronous processing by using the async and await keywords.
- to convert any lambda expression to a delegate type.

More details can be found here: Lambda expressions - C# reference | Microsoft Learn[^]
Share this answer
Southmountain 29-Nov-22 23:45pm    
thanks for confirmation!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900