PYnative

Python Programming

  • Learn Python ▼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises ▼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • Quizzes
  • Code Editor ▼
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » C# Exercises » C# Lambda Expressions Exercises: 25 Coding Problems with Solutions

C# Lambda Expressions Exercises: 25 Coding Problems with Solutions

Updated on: July 15, 2026 | Leave a Comment

Lambda expressions let you write short, inline functions without the overhead of defining a named method, and they’re the foundation behind LINQ and much of modern C#’s functional-style code.

This collection of 20 C# lambda expression exercises covers writing lambdas with Func<T>, Action<T>, and Predicate<T>, and using them to filter, transform, and process data concisely.

Each exercise includes a Practice Problem, Purpose, Hint, and a full Solution with Explanation, so you get comfortable reading and writing lambda syntax with confidence.

Also, See: C# Exercises with  22 topic-wise sets and 620+ practice questions.

What You’ll Practice

  • Syntax: Single-expression and multi-line lambda bodies.
  • Delegate Types: Func<T, TResult>, Action<T>, and Predicate<T>.
  • Practical Use: Passing lambdas to LINQ methods and collection operations.
  • Closures: Capturing outer variables inside a lambda expression.
+ Table Of Contents (20 Exercises)

Table of contents

  • Exercise 1: Square a Number
  • Exercise 2: Check for Even Number
  • Exercise 3: String Length Checker
  • Exercise 4: Print Message with Log Prefix
  • Exercise 5: Concatenate Two Strings
  • Exercise 6: Filter Odd Numbers from a List
  • Exercise 7: Transform Strings to Uppercase
  • Exercise 8: Find First Product Over $100
  • Exercise 9: Sort Strings by Length
  • Exercise 10: Extract Emails from a User List
  • Exercise 11: Check Any Condition
  • Exercise 12: Count Specific Elements
  • Exercise 13: Custom Object Transformation
  • Exercise 14: Group by First Letter
  • Exercise 15: Conditional Aggregation
  • Exercise 16: Inline Multi-statement Lambda
  • Exercise 17: Dictionary Filtering
  • Exercise 18: Inline Event Subscription
  • Exercise 19: Closure and Captured Variables
  • Exercise 20: Inspect an Expression Tree

Exercise 1: Square a Number

Practice Problem: Write a lambda expression assigned to a Func<int, int> that takes an integer and returns its square.

Purpose: This exercise helps you practice assigning a lambda expression to a Func delegate and invoking it like a regular method, a foundational pattern for functional style programming in C#.

Given Input: number = 6

Expected Output:

Number = 6
Square = 36
▼ Hint
  • Declare a variable of type Func<int, int> and assign a lambda expression to it.
  • The lambda body should multiply the input parameter by itself.
  • Call the delegate like a normal method by passing the number in parentheses.
▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int> square = x => x * x;

            int number = 6;
            int result = square(number);

            Console.WriteLine($"Number = {number}");
            Console.WriteLine($"Square = {result}");
        }
    }
}Code language: C# (cs)

Explanation:

  • Func<int, int> square = x => x * x;: Declares a delegate that takes an int and returns an int, with the lambda body computing the square.
  • square(number): Invokes the lambda expression by calling the delegate variable as if it were a method.
  • $"Square = {result}": Uses string interpolation to embed the computed result directly in the output text.

Exercise 2: Check for Even Number

Practice Problem: Create a Func<int, bool> lambda that returns true if a given number is even, and false if it is odd.

Purpose: This exercise helps you practice writing a lambda expression that returns a boolean based on a condition, a pattern used constantly in filtering and validation logic.

Given Input: number = 15

Expected Output:

Number = 15
Is Even = False
▼ Hint

Use the modulus operator % inside the lambda body to check whether the remainder of dividing by 2 is zero.

▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, bool> isEven = x => x % 2 == 0;

            int number = 15;
            bool result = isEven(number);

            Console.WriteLine($"Number = {number}");
            Console.WriteLine($"Is Even = {result}");
        }
    }
}Code language: C# (cs)

Explanation:

  • Func<int, bool> isEven = x => x % 2 == 0;: Declares a delegate that takes an int and returns a bool based on whether the number is divisible by 2.
  • x % 2 == 0: Checks the remainder of dividing x by 2, which is zero only for even numbers.
  • isEven(number): Calls the lambda with the given number and stores the boolean result.

Exercise 3: String Length Checker

Practice Problem: Write a lambda expression that takes a string and an integer, returning true if the string’s length is greater than the integer.

Purpose: This exercise helps you practice writing a lambda expression with two parameters of different types, useful when validating input against a length threshold.

Given Input: word = "Programming", minLength = 5

Expected Output:

Word = Programming
Length = 11
Is Longer Than 5 = True
▼ Hint
  • Use Func<string, int, bool> since the lambda takes two input parameters and returns a boolean.
  • Compare text.Length against the passed in length value using the > operator.
▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, int, bool> isLongerThan = (text, length) => text.Length > length;

            string word = "Programming";
            int minLength = 5;
            bool result = isLongerThan(word, minLength);

            Console.WriteLine($"Word = {word}");
            Console.WriteLine($"Length = {word.Length}");
            Console.WriteLine($"Is Longer Than {minLength} = {result}");
        }
    }
}Code language: C# (cs)

Explanation:

  • Func<string, int, bool>: Declares a delegate with two input parameters, a string and an int, that returns a bool.
  • (text, length) => text.Length > length: A multi parameter lambda that compares the string’s length against the given threshold.
  • isLongerThan(word, minLength): Calls the delegate with both arguments in the order they were declared.

Exercise 4: Print Message with Log Prefix

Practice Problem: Create an Action<string> lambda that prepends "[Log]: " to a string and prints it to the console.

Purpose: This exercise helps you practice using the Action delegate for lambdas that perform a side effect, such as printing, rather than returning a value.

Given Input: message = "Application started successfully."

Expected Output: [Log]: Application started successfully.

▼ Hint

Action<string> is used instead of Func because the lambda does not return a value, it only performs an action like printing.

▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<string> logMessage = message => Console.WriteLine($"[Log]: {message}");

            logMessage("Application started successfully.");
        }
    }
}Code language: C# (cs)

Explanation:

  • Action<string> logMessage: Declares a delegate that takes a string parameter and returns nothing, suited for operations like printing.
  • message => Console.WriteLine($"[Log]: {message}"): The lambda body prepends the log prefix and writes the combined text to the console.
  • logMessage("Application started successfully."): Invokes the action with the message to be logged.

Exercise 5: Concatenate Two Strings

Practice Problem: Write a lambda expression assigned to a Func<string, string, string> that joins two strings with a space in between.

Purpose: This exercise helps you practice writing a lambda that takes multiple parameters of the same type and returns a combined value, common when building names or formatted text.

Given Input: firstName = "John", lastName = "Smith"

Expected Output: Full Name = John Smith

▼ Hint

Use string interpolation inside the lambda body to place a space between the two parameters, for example $"{first} {second}".

▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string, string> joinStrings = (first, second) => $"{first} {second}";

            string firstName = "John";
            string lastName = "Smith";
            string fullName = joinStrings(firstName, lastName);

            Console.WriteLine($"Full Name = {fullName}");
        }
    }
}Code language: C# (cs)

Explanation:

  • Func<string, string, string>: Declares a delegate that takes two string parameters and returns a string.
  • (first, second) => $"{first} {second}": The lambda body uses string interpolation to join both parameters with a single space.
  • joinStrings(firstName, lastName): Calls the delegate with the two string values to produce the combined result.

Exercise 6: Filter Odd Numbers from a List

Practice Problem: Given a List<int>, use .Where() with a lambda expression to filter out all odd numbers.

Purpose: This exercise helps you practice using LINQ’s Where() method with a lambda predicate to filter a collection, a common pattern in data processing.

Given Input: numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

Expected Output:

Odd Numbers:
1
3
5
7
9
▼ Hint
  • Call .Where() on the list and pass a lambda that checks n % 2 != 0.
  • Chain .ToList() after .Where() to convert the filtered result back into a list.
  • Remember to add using System.Linq; at the top of the file.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            List<int> oddNumbers = numbers.Where(n => n % 2 != 0).ToList();

            Console.WriteLine("Odd Numbers:");
            foreach (int num in oddNumbers)
            {
                Console.WriteLine(num);
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • numbers.Where(n => n % 2 != 0): Filters the list, keeping only elements where the lambda condition evaluates to true.
  • .ToList(): Converts the filtered sequence returned by Where() into a concrete List<int>.
  • foreach (int num in oddNumbers): Iterates through the filtered list and prints each odd number on its own line.

Exercise 7: Transform Strings to Uppercase

Practice Problem: Given a List<string>, use .Select() with a lambda to convert all strings in the list to uppercase.

Purpose: This exercise helps you practice using LINQ’s Select() method to project each element of a collection into a new transformed value.

Given Input: fruits = { "apple", "banana", "cherry" }

Expected Output:

Uppercase Fruits:
APPLE
BANANA
CHERRY
▼ Hint
  • Call .Select() on the list and pass a lambda that calls .ToUpper() on each string.
  • Chain .ToList() after .Select() to materialize the projected sequence into a list.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> fruits = new List<string> { "apple", "banana", "cherry" };

            List<string> upperFruits = fruits.Select(f => f.ToUpper()).ToList();

            Console.WriteLine("Uppercase Fruits:");
            foreach (string fruit in upperFruits)
            {
                Console.WriteLine(fruit);
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • fruits.Select(f => f.ToUpper()): Projects each string in the list into its uppercase equivalent using the lambda.
  • .ToList(): Converts the projected sequence returned by Select() into a concrete List<string>.
  • foreach (string fruit in upperFruits): Iterates through the transformed list and prints each uppercase fruit name.

Exercise 8: Find First Product Over $100

Practice Problem: Given a list of Product objects (with Name and Price), use .FirstOrDefault() with a lambda to find the first product that costs more than $100.

Purpose: This exercise helps you practice using LINQ’s FirstOrDefault() method with a lambda predicate to search a collection of objects for the first match based on a property value.

Given Input: A list of four products with prices 45.50, 199.99, 25.00, and 899.00.

Expected Output:

Name = Monitor
Price = 199.99
▼ Hint
  • Define a simple Product class with Name and Price properties before Main().
  • Call .FirstOrDefault() on the list with a lambda that checks p.Price > 100.
  • FirstOrDefault() returns null if no match is found, so check for null before printing.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Product> products = new List<Product>
            {
                new Product { Name = "Keyboard", Price = 45.50 },
                new Product { Name = "Monitor", Price = 199.99 },
                new Product { Name = "Mouse", Price = 25.00 },
                new Product { Name = "Laptop", Price = 899.00 }
            };

            Product firstExpensive = products.FirstOrDefault(p => p.Price > 100);

            if (firstExpensive != null)
            {
                Console.WriteLine($"Name = {firstExpensive.Name}");
                Console.WriteLine($"Price = {firstExpensive.Price}");
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • products.FirstOrDefault(p => p.Price > 100): Searches the list and returns the first product whose Price exceeds 100, or null if none match.
  • if (firstExpensive != null): Guards against a null reference before accessing the result’s properties.
  • firstExpensive.Name: Accesses the Name property of the matched Product object for display.

Exercise 9: Sort Strings by Length

Practice Problem: Given a list of strings, use .OrderBy() or .OrderByDescending() with a lambda to sort the list by the length of the strings.

Purpose: This exercise helps you practice using LINQ’s OrderBy() method with a lambda key selector to sort a collection based on a computed value rather than the elements themselves.

Given Input: words = { "kiwi", "banana", "fig", "watermelon", "pear" }

Expected Output:

Sorted by Length:
fig
kiwi
pear
banana
watermelon
▼ Hint
  • Call .OrderBy() on the list and pass a lambda that returns w.Length as the sort key.
  • Use .OrderByDescending() instead if you want the longest strings first.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> words = new List<string> { "kiwi", "banana", "fig", "watermelon", "pear" };

            List<string> sortedWords = words.OrderBy(w => w.Length).ToList();

            Console.WriteLine("Sorted by Length:");
            foreach (string word in sortedWords)
            {
                Console.WriteLine(word);
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • words.OrderBy(w => w.Length): Sorts the list in ascending order using each string’s length as the key, computed by the lambda.
  • .ToList(): Converts the ordered sequence returned by OrderBy() into a concrete List<string>.
  • foreach (string word in sortedWords): Iterates through the sorted list and prints each word in order of increasing length.

Exercise 10: Extract Emails from a User List

Practice Problem: Given a List<User> (with properties Id, Username, Email), use .Select() to extract a list of just the Email strings.

Purpose: This exercise helps you practice using LINQ’s Select() method to project a list of objects down to a single property, a common step when preparing data for display or export.

Given Input: A list of three users, each with an Id, Username, and Email.

Expected Output:

Emails:
alice@example.com
bob@example.com
carol@example.com
▼ Hint
  • Define a simple User class with Id, Username, and Email properties before Main().
  • Call .Select() on the list with a lambda that returns u.Email for each user.
  • Chain .ToList() to store the projected emails as a List<string>.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Email { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<User> users = new List<User>
            {
                new User { Id = 1, Username = "alice", Email = "alice@example.com" },
                new User { Id = 2, Username = "bob", Email = "bob@example.com" },
                new User { Id = 3, Username = "carol", Email = "carol@example.com" }
            };

            List<string> emails = users.Select(u => u.Email).ToList();

            Console.WriteLine("Emails:");
            foreach (string email in emails)
            {
                Console.WriteLine(email);
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • users.Select(u => u.Email): Projects each User object down to just its Email property using the lambda.
  • .ToList(): Converts the projected sequence returned by Select() into a concrete List<string>.
  • foreach (string email in emails): Iterates through the extracted list and prints each email address on its own line.

Exercise 11: Check Any Condition

Practice Problem: Given a list of integers, use .Any() with a lambda to check if the list contains any negative numbers.

Purpose: This exercise helps you practice using LINQ’s Any() method with a lambda predicate to test whether at least one element in a collection satisfies a condition.

Given Input: numbers = { 4, 8, -3, 15, 22, -7 }

Expected Output: Has Negative Number = True

▼ Hint

Call .Any() on the list and pass a lambda that checks whether each number is less than zero, for example n => n < 0.

▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int> { 4, 8, -3, 15, 22, -7 };

            bool hasNegative = numbers.Any(n => n < 0);

            Console.WriteLine($"Has Negative Number = {hasNegative}");
        }
    }
}Code language: C# (cs)

Explanation:

  • numbers.Any(n => n < 0): Checks every element against the lambda condition and returns true as soon as one negative number is found.
  • Short circuit: Any() stops evaluating as soon as it finds a match, making it more efficient than counting all matches.

Exercise 12: Count Specific Elements

Practice Problem: Given a list of strings, use .Count() with a lambda to count how many strings start with the letter ‘A’.

Purpose: This exercise helps you practice using LINQ’s Count() overload that accepts a lambda predicate, letting you count matching elements without writing a manual loop.

Given Input: names = { "Alice", "Bob", "Amanda", "Charlie", "Andrew" }

Expected Output: Names Starting With A = 3

▼ Hint

Use the StartsWith() string method inside the lambda passed to .Count(), for example n => n.StartsWith("A").

▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string> { "Alice", "Bob", "Amanda", "Charlie", "Andrew" };

            int countStartingWithA = names.Count(n => n.StartsWith("A"));

            Console.WriteLine($"Names Starting With A = {countStartingWithA}");
        }
    }
}Code language: C# (cs)

Explanation:

  • names.Count(n => n.StartsWith("A")): Evaluates the lambda against every element and returns the total number of strings for which it returns true.
  • n.StartsWith("A"): Performs a case sensitive check for whether the string begins with the letter A.

Exercise 13: Custom Object Transformation

Practice Problem: Given a List<Employee>, use .Select() with a lambda to project them into an anonymous type containing only FullName (combined FirstName and LastName) and IsSenior (true if YearsOfExperience > 5).

Purpose: This exercise helps you practice projecting objects into a new shape using an anonymous type inside a lambda, a common technique when preparing view specific or report specific data.

Given Input: Three employees with years of experience 3, 8, and 6.

Expected Output:

Sara Khan - Senior: False
Tom Reed - Senior: True
Nina Patel - Senior: True
▼ Hint
  • Inside the lambda passed to .Select(), use new { ... } to build an anonymous type with the two computed properties.
  • Declare the result with var since anonymous types do not have a named type you can reference directly.
  • Combine FirstName and LastName with string interpolation, and compare YearsOfExperience to 5 for IsSenior.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int YearsOfExperience { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>
            {
                new Employee { FirstName = "Sara", LastName = "Khan", YearsOfExperience = 3 },
                new Employee { FirstName = "Tom", LastName = "Reed", YearsOfExperience = 8 },
                new Employee { FirstName = "Nina", LastName = "Patel", YearsOfExperience = 6 }
            };

            var summaries = employees.Select(e => new
            {
                FullName = $"{e.FirstName} {e.LastName}",
                IsSenior = e.YearsOfExperience > 5
            }).ToList();

            foreach (var summary in summaries)
            {
                Console.WriteLine($"{summary.FullName} - Senior: {summary.IsSenior}");
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • new { FullName = ..., IsSenior = ... }: Creates an anonymous type on the fly with only the two properties the lambda needs to produce.
  • var summaries: Uses var because the compiler generated anonymous type has no name that can be written explicitly.
  • e.YearsOfExperience > 5: Computes the IsSenior flag directly inside the projection instead of storing it as a separate step.

Exercise 14: Group by First Letter

Practice Problem: Given a list of words, use .GroupBy() with a lambda to group the words by their first letter, then print the groups.

Purpose: This exercise helps you practice using LINQ’s GroupBy() method with a lambda key selector to organize a flat collection into related buckets.

Given Input: words = { "apple", "avocado", "banana", "blueberry", "cherry" }

Expected Output:

a: apple, avocado
b: banana, blueberry
c: cherry
▼ Hint
  • Call .GroupBy() with a lambda that returns the first character of each word as the grouping key.
  • Each resulting group has a Key property and can itself be enumerated like a list.
  • Use string.Join(", ", group) to combine the words in a group into a single readable line.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> words = new List<string> { "apple", "avocado", "banana", "blueberry", "cherry" };

            var groups = words.GroupBy(w => w[0]);

            foreach (var group in groups)
            {
                Console.WriteLine($"{group.Key}: {string.Join(", ", group)}");
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • words.GroupBy(w => w[0]): Groups the words using the first character of each string as the key, computed by the lambda.
  • group.Key: Holds the shared first letter for all words inside that particular group.
  • string.Join(", ", group): Concatenates all the words within a group into a single comma separated line.

Exercise 15: Conditional Aggregation

Practice Problem: Given a List<Order> (with Amount and IsPaid), use .Sum() with a lambda to calculate the total revenue generated only from paid orders.

Purpose: This exercise helps you practice using LINQ’s Sum() method with a lambda that applies conditional logic inline, avoiding a separate filter step before aggregating.

Given Input: Four orders with amounts 120.50, 75.00, 200.00, and 40.25, where the first and third are marked paid.

Expected Output: Total Paid Revenue = 320.5

▼ Hint
  • Use the ternary operator inside the lambda to return o.Amount when o.IsPaid is true, and 0 otherwise.
  • .Sum() adds up whatever value the lambda returns for each element in the list.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Order
    {
        public double Amount { get; set; }
        public bool IsPaid { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Order> orders = new List<Order>
            {
                new Order { Amount = 120.50, IsPaid = true },
                new Order { Amount = 75.00, IsPaid = false },
                new Order { Amount = 200.00, IsPaid = true },
                new Order { Amount = 40.25, IsPaid = false }
            };

            double totalPaidRevenue = orders.Sum(o => o.IsPaid ? o.Amount : 0);

            Console.WriteLine($"Total Paid Revenue = {totalPaidRevenue}");
        }
    }
}Code language: C# (cs)

Explanation:

  • orders.Sum(o => o.IsPaid ? o.Amount : 0): Adds each order’s Amount to the running total only when IsPaid is true, contributing zero otherwise.
  • o.IsPaid ? o.Amount : 0: A ternary expression that acts as an inline filter within the aggregation itself.

Exercise 16: Inline Multi-statement Lambda

Practice Problem: Write a Func<int, int, int> using a statement block { ... } that compares two numbers, logs the larger number to the console, and then returns the absolute difference between them.

Purpose: This exercise helps you practice writing a statement lambda that contains multiple lines of logic instead of a single expression, useful when a lambda needs to perform a side effect before returning a value.

Given Input: first = 18, second = 42

Expected Output:

Larger Number = 42
Absolute Difference = 24
▼ Hint
  • A statement lambda wraps its body in curly braces and requires an explicit return statement.
  • Use the ternary operator to determine the larger value before printing it.
  • Use Math.Abs() on the subtraction of the two numbers to compute the absolute difference.
▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int, int> compareAndDiff = (a, b) =>
            {
                int larger = a > b ? a : b;
                Console.WriteLine($"Larger Number = {larger}");
                return Math.Abs(a - b);
            };

            int first = 18;
            int second = 42;
            int difference = compareAndDiff(first, second);

            Console.WriteLine($"Absolute Difference = {difference}");
        }
    }
}Code language: C# (cs)

Explanation:

  • (a, b) => { ... }: A statement lambda with a curly brace body, allowing multiple statements before the final return.
  • Console.WriteLine($"Larger Number = {larger}"): Executes a side effect inside the lambda before the return statement runs.
  • Math.Abs(a - b): Computes the absolute difference regardless of which of the two numbers is larger.

Exercise 17: Dictionary Filtering

Practice Problem: Given a Dictionary<string, int> representing items and their stock counts, use LINQ and a lambda to filter out items that are out of stock (count == 0).

Purpose: This exercise helps you practice applying LINQ’s Where() method to a dictionary, where each element is a KeyValuePair and the lambda inspects the Value to decide what stays.

Given Input: A dictionary with Keyboard: 12, Mouse: 0, Monitor: 5, Webcam: 0.

Expected Output:

In Stock Items:
Keyboard: 12
Monitor: 5
▼ Hint
  • Call .Where() directly on the dictionary, since it can be enumerated as a sequence of KeyValuePair<string, int> entries.
  • Inside the lambda, check item.Value > 0 to keep only items that still have stock.
  • Access item.Key and item.Value when printing each remaining entry.
▼ Solution & Explanation
using System;
using System.Collections.Generic;
using System.Linq;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> stock = new Dictionary<string, int>
            {
                { "Keyboard", 12 },
                { "Mouse", 0 },
                { "Monitor", 5 },
                { "Webcam", 0 }
            };

            var inStockItems = stock.Where(item => item.Value > 0);

            Console.WriteLine("In Stock Items:");
            foreach (var item in inStockItems)
            {
                Console.WriteLine($"{item.Key}: {item.Value}");
            }
        }
    }
}Code language: C# (cs)

Explanation:

  • stock.Where(item => item.Value > 0): Filters the dictionary entries, keeping only those whose stock count is greater than zero.
  • item.Key and item.Value: Access the item name and its remaining stock count from each KeyValuePair.

Exercise 18: Inline Event Subscription

Practice Problem: Given a custom class with an public event EventHandler ThresholdReached, use a lambda expression to subscribe to the event and print a message when it fires.

Purpose: This exercise helps you practice subscribing to an event using a lambda instead of a separately declared method, which keeps small event handlers close to where they are used.

Given Input: temperature = 105

Expected Output: Warning: Temperature threshold reached!

▼ Hint
  • Use += to subscribe a lambda directly to the event, matching the EventHandler signature of (sender, e).
  • Use the null conditional operator ?.Invoke() when raising the event, so nothing happens if no one has subscribed.
▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class TemperatureSensor
    {
        public event EventHandler ThresholdReached;

        public void CheckTemperature(int temperature)
        {
            if (temperature > 100)
            {
                ThresholdReached?.Invoke(this, EventArgs.Empty);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TemperatureSensor sensor = new TemperatureSensor();

            sensor.ThresholdReached += (sender, e) => Console.WriteLine("Warning: Temperature threshold reached!");

            sensor.CheckTemperature(105);
        }
    }
}Code language: C# (cs)

Explanation:

  • sensor.ThresholdReached += (sender, e) => ...: Subscribes a lambda directly to the event without declaring a separate named method.
  • ThresholdReached?.Invoke(this, EventArgs.Empty): Raises the event, running every subscribed lambda, but only if at least one subscriber exists.
  • if (temperature > 100): Determines whether the threshold condition is met before the event is raised.

Exercise 19: Closure and Captured Variables

Practice Problem: Create a method that returns a Func<int, int>. Inside the method, capture a local variable factor. The returned lambda should multiply its input by factor. Demonstrate how changing factor after defining the lambda affects the outcome.

Purpose: This exercise helps you practice closures, where a lambda captures a variable from its enclosing scope and keeps a reference to it even after the method that created it has returned.

Given Input: factor = 3, later reassigned to factor = 10

Expected Output:

5 multiplied by factor = 15
5 multiplied by factor after change = 15
▼ Hint
  • Write a method that takes factor as a parameter and returns a lambda referencing that parameter.
  • The lambda captures the factor parameter belonging to that specific method call, not the outer variable used to call the method.
  • Reassigning the outer factor variable in Main() after the lambda is created has no effect on the already returned lambda.
▼ Solution & Explanation
using System;

namespace LambdaExercises
{
    class Program
    {
        static Func<int, int> CreateMultiplier(int factor)
        {
            return number => number * factor;
        }

        static void Main(string[] args)
        {
            int factor = 3;
            Func<int, int> multiplyByFactor = CreateMultiplier(factor);

            Console.WriteLine($"5 multiplied by factor = {multiplyByFactor(5)}");

            factor = 10;
            Console.WriteLine($"5 multiplied by factor after change = {multiplyByFactor(5)}");
        }
    }
}Code language: C# (cs)

Explanation:

  • return number => number * factor;: The returned lambda forms a closure over the factor parameter of CreateMultiplier, keeping it alive after the method returns.
  • CreateMultiplier(factor): Passes the value of the outer factor at the time of the call, which becomes a separate copy inside the method.
  • Unaffected by reassignment: Setting factor = 10 in Main() only changes the outer local variable, not the copy captured inside CreateMultiplier, so the lambda still multiplies by 3.

Exercise 20: Inspect an Expression Tree

Practice Problem: Instead of Func<int, bool>, assign a lambda expression to an Expression<Func<int, bool>> that checks if a number is greater than 5. Write a small script to parse and print the node types of the expression tree.

Purpose: This exercise helps you practice the difference between a compiled lambda delegate and an expression tree, which represents the lambda as inspectable data rather than executable code, a technique used by LINQ providers such as Entity Framework.

Given Input: isGreaterThanFive = number => number > 5

Expected Output:

Expression = number => (number > 5)
Body Node Type = GreaterThan
Left Node Type = Parameter
Right Node Type = Constant
▼ Hint
  • Add using System.Linq.Expressions; so the Expression<T> type and related classes are available.
  • An Expression<Func<int, bool>> is not compiled automatically, it stays as a tree describing the logic, accessible through its Body.
  • Cast Body to BinaryExpression to inspect its Left and Right sub expressions and their NodeType.
▼ Solution & Explanation
using System;
using System.Linq.Expressions;

namespace LambdaExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Expression<Func<int, bool>> isGreaterThanFive = number => number > 5;

            Console.WriteLine($"Expression = {isGreaterThanFive}");
            Console.WriteLine($"Body Node Type = {isGreaterThanFive.Body.NodeType}");

            BinaryExpression body = (BinaryExpression)isGreaterThanFive.Body;
            Console.WriteLine($"Left Node Type = {body.Left.NodeType}");
            Console.WriteLine($"Right Node Type = {body.Right.NodeType}");
        }
    }
}Code language: C# (cs)

Explanation:

  • Expression<Func<int, bool>>: Stores the lambda as a tree of expression objects instead of compiling it directly into executable code.
  • isGreaterThanFive.Body: Represents the root of the expression tree, in this case the greater than comparison.
  • (BinaryExpression)isGreaterThanFive.Body: Casts the body so its Left and Right operands, the parameter and the constant 5, can be inspected individually.

Filed Under: C# Exercises

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

C# Exercises

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: C# Exercises
TweetF  sharein  shareP  Pin

  C# Exercises

  • C# Exercise for Beginners
  • C# Loops Exercise
  • C# Array Exercise
  • C# String Exercise
  • C# OOP Exercise
  • C# Structs, Records and Enums Exercise
  • C# Collections Exercise
  • C# List Exercise
  • C# Dictionary Exercise
  • C# LINQ Exercise
  • C# Exception Handling Exercise
  • C# File Handling Exercise
  • C# Date and Time Exercise
  • C# Generics Exercise
  • C# Lambda Expressions Exercise
  • C# Delegates and Events Exercise
  • C# Extension Methods Exercise
  • C# Regex Exercise
  • C# Pattern Matching Exercise
  • C# Iterators and Yield Exercise
  • C# Indexers and Operator Overloading Exercise
  • C# Random Data Generation Exercise

All Coding Exercises

Python Exercises C Exercises C++ Exercises Java Exercises C# Exercises

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com