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

Predicate delegate type in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Predicate Delegate Type in C#
📖 Scenario: You are working on a simple program to filter a list of numbers. You want to find which numbers are even using a special kind of function called a Predicate. This helps you check each number and decide if it meets your rule.
🎯 Goal: Build a C# program that uses a Predicate<int> delegate to find all even numbers from a list of integers.
📋 What You'll Learn
Create a list of integers with exact values: 1, 2, 3, 4, 5, 6
Create a Predicate<int> delegate called isEven that checks if a number is even
Use the FindAll method with the isEven predicate to get all even numbers
Print the resulting list of even numbers separated by spaces
💡 Why This Matters
🌍 Real World
Filtering lists based on conditions is common in apps like shopping filters, contact lists, or game scores.
💼 Career
Understanding Predicate delegates helps you write clean, reusable code for filtering data in C# applications.
Progress0 / 4 steps
1
Create the list of numbers
Create a List<int> called numbers with these exact values: 1, 2, 3, 4, 5, 6.
C Sharp (C#)
Need a hint?

Use new List<int> { 1, 2, 3, 4, 5, 6 } to create the list.

2
Create the Predicate delegate
Create a Predicate<int> delegate called isEven that returns true if a number is even (divisible by 2), otherwise false.
C Sharp (C#)
Need a hint?

Use a lambda expression like n => n % 2 == 0 to define the predicate.

3
Use FindAll with the Predicate
Use the FindAll method on numbers with the isEven predicate to create a new list called evenNumbers containing only even numbers.
C Sharp (C#)
Need a hint?

Call numbers.FindAll(isEven) to get the filtered list.

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

Use Console.WriteLine(string.Join(" ", evenNumbers)) to print the list.