0
0
C Sharp (C#)programming~30 mins

Custom LINQ extension methods in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom LINQ Extension Methods
📖 Scenario: You work with lists of numbers and want to create your own special filters and operations, just like LINQ does. This helps you reuse code and write cleaner programs.
🎯 Goal: Build a custom LINQ extension method called IsEven that filters only even numbers from a list of integers.
📋 What You'll Learn
Create a list of integers called numbers with the values 1, 2, 3, 4, 5, 6
Create a boolean extension method called IsEven for integers
Use the IsEven method in a LINQ Where clause to filter even numbers
Print the filtered even numbers separated by spaces
💡 Why This Matters
🌍 Real World
Custom LINQ extension methods help you add your own reusable filters and operations to collections, making your code cleaner and easier to read.
💼 Career
Many software jobs require writing clean, reusable code. Knowing how to create and use extension methods is a valuable skill for working with collections and LINQ in C#.
Progress0 / 4 steps
1
Create the list of numbers
Create a list of integers called numbers with these exact values: 1, 2, 3, 4, 5, 6.
C Sharp (C#)
Need a hint?

Use List<int> and initialize it with the values inside curly braces.

2
Create the IsEven extension method
Create a public static class called Extensions. Inside it, create a public static method called IsEven that takes this int number as a parameter and returns true if the number is even, otherwise false.
C Sharp (C#)
Need a hint?

Extension methods must be inside a static class and have this before the first parameter.

3
Use IsEven in a LINQ Where clause
Inside Main, create a variable called evenNumbers that uses numbers.Where(n => n.IsEven()) to filter only even numbers.
C Sharp (C#)
Need a hint?

Use Where with a lambda expression calling IsEven() on each number.

4
Print the filtered even numbers
Print the evenNumbers separated by spaces using Console.WriteLine(string.Join(" ", evenNumbers)).
C Sharp (C#)
Need a hint?

Use Console.WriteLine with string.Join to print the numbers separated by spaces.