Techiehook Techiehook
Updated date Jun 20, 2024
This article provides lambda expressions in C# .NET 8, providing their syntax and practical uses in LINQ queries, and event handling, and delegates with examples and output.

Lambda Expressions in C#

In .NET 8, lambda expressions are used in the same way as in previous versions. They are often used with LINQ queries, event handling, and various functional programming tasks. 

Basic Syntax

The basic syntax of a lambda expression in C# is:

(parameters) => expression or statement_block

In this article, we will see various methods to use the lambda expression in C#.

Using Lambda Expressions with LINQ

In the below program, we used lambda expressions with LINQ to filter and select data from a list.

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14 };
        
        var filteredNumbers = numbers.Where(n => n > 6);

        Console.WriteLine("Numbers greater than 6:");
        foreach (var number in filteredNumbers)
        {
            Console.WriteLine(number);
        }

        // Selecting square of each number
        var squares = numbers.Select(n => n * n);

        Console.WriteLine("\nSquares of numbers:");
        foreach (var square in squares)
        {
            Console.WriteLine(square);
        }
        Console.ReadKey();
    }
}

Output:

Numbers greater than 6:
7
8
9
10

Squares of numbers:
1
4
9
16
25
36
49
64
81
100

Event Handling with Lambda Expressions

We used lambda expressions with event handling as shown below,

using System;

public class Button
{
    public event EventHandler Click;

    public void SimulateClick()
    {
        if (Click != null)
        {
            Click(this, EventArgs.Empty);
        }
    }
}

public class Program
{
    public static void Main()
    {
        Button button = new Button();
        button.Click += (sender, e) => Console.WriteLine("The Button was clicked!");

        // Simulate a button click
        button.SimulateClick();
        Console.ReadKey();
    }
}

Output:

The Button was clicked!

Lambda Expressions as Delegates

We can use lambda expressions to create delegates as shown below,

using System;

public class Program
{
    public delegate int MathOperation(int a, int b);

    public static void Main()
    {
        MathOperation add = (a, b) => a + b;
        MathOperation multiply = (a, b) => a * b;

        int sum = add(10, 13);
        int product = multiply(8, 5);

        Console.WriteLine($"Sum: {sum}");
        Console.WriteLine($"Product: {product}");
        Console.ReadKey();
    }
}

Output:

Sum: 23
Product: 40

Complex Expressions and Statements

Lambda expressions also can contain multiple statements enclosed in braces, please see the below program to understand the lambda expressions.

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // Complex lambda expression with multiple statements
        var result = numbers.Where(n =>
        {
            Console.WriteLine($"Processing number: {n}");
            return n % 2 == 0;
        });

        Console.WriteLine("\nEven numbers:");
        foreach (var number in result)
        {
            Console.WriteLine(number);
        }
        Console.ReadKey();
    }
}

Output:

Even numbers:
Processing number: 1
Processing number: 2
2
Processing number: 3
Processing number: 4
4
Processing number: 5

 

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!