A Predicate delegate helps you check if something is true or false about an item. It makes your code cleaner when you want to test conditions.
Predicate delegate type in C Sharp (C#)
Predicate<T> predicateName = MethodName;
// or using lambda expression
Predicate<T> predicateName = item => condition;T is the type of item you want to test.
The method or lambda must return true or false.
IsEven to check if a number is even.Predicate<int> isEven = IsEven; bool IsEven(int number) { return number % 2 == 0; }
Predicate<string> isLong = s => s.Length > 5;This program creates a list of numbers and uses a Predicate delegate to find all even numbers. It then prints them.
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 }; Predicate<int> isEven = n => n % 2 == 0; List<int> evenNumbers = numbers.FindAll(isEven); Console.WriteLine("Even numbers:"); foreach (int num in evenNumbers) { Console.WriteLine(num); } } }
You can use Predicate with any type, not just numbers or strings.
Predicate is often used with collection methods like FindAll, Exists, and RemoveAll.
Using lambda expressions makes your code shorter and easier to read.
Predicate delegate tests if an item meets a condition and returns true or false.
It helps write cleaner and reusable condition checks.
Commonly used with collections to find or filter items.
Practice
What does a Predicate<T> delegate represent in C#?
Solution
Step 1: Understand the Predicate delegate signature
A Predicate delegate always takes one parameter of type T and returns a boolean value.Step 2: Match the description to the options
A method that takes a parameter of type T and returns a boolean indicating a condition correctly describes this signature as a method taking T and returning bool.Final Answer:
A method that takes a parameter of type T and returns a boolean indicating a condition. -> Option AQuick Check:
Predicate<T> = method(T) returns bool [OK]
- Confusing Predicate with Action or Func delegates
- Thinking Predicate returns void or other types
- Assuming Predicate takes multiple parameters
Which of the following is the correct syntax to declare a Predicate delegate for integers?
?
Solution
Step 1: Check the lambda expression return type
Predicate<int> expects a method returning bool. Predicate<int> isPositive = x => x > 0; returns x > 0, which is bool.Step 2: Verify parameter and return correctness
Predicate<int> isPositive = (int x) => { return x; }; returns x (int), not bool. Predicate<int> isPositive = x => x + 1; returns x + 1 (int), not bool. Predicate<int> isPositive = () => true; has no parameter but Predicate requires one.Final Answer:
Predicate<int> isPositive = x => x > 0; -> Option AQuick Check:
Predicate<int> = int input => bool output [OK]
- Returning int instead of bool
- Missing parameter in lambda
- Using incorrect lambda syntax
What will be the output of the following C# code?
using System;
class Program {
static void Main() {
Predicate<string> isLong = s => s.Length > 5;
string[] words = {"apple", "banana", "pear"};
int count = 0;
foreach (var word in words) {
if (isLong(word)) count++;
}
Console.WriteLine(count);
}
}Solution
Step 1: Understand the Predicate condition
The predicate checks if the string length is greater than 5.Step 2: Count words with length > 5
"apple" length 5 (false), "banana" length 6 (true), "pear" length 4 (false). Only "banana" passes.Final Answer:
2 -> Option BQuick Check:
Words with length > 5 are "banana" only, count = 1 [Incorrect]
- Counting words with length equal to 5
- Misreading predicate condition
- Assuming all words pass the test
Identify the error in this code using a Predicate delegate:
Predicate<int> isEven = n => n % 2 == 0;
int[] numbers = {1, 2, 3, 4};
int evenCount = 0;
foreach (int num in numbers) {
if (isEven) evenCount++;
}
Console.WriteLine(evenCount);Solution
Step 1: Check how Predicate is used in if condition
The code usesif (isEven)which checks the delegate itself, not its result.Step 2: Correct usage requires calling the delegate with an argument
It should beif (isEven(num))to evaluate the condition for each number.Final Answer:
The if condition should call the delegate with an argument. -> Option CQuick Check:
Call Predicate with input: isEven(num) [OK]
- Using delegate name without parentheses
- Confusing delegate variable with method call
- Ignoring parameter requirement
You want to filter a list of integers to keep only positive even numbers using a Predicate delegate. Which of the following implementations correctly combines these conditions?
Solution
Step 1: Understand the filtering requirement
We want numbers that are both positive and even.Step 2: Analyze each option's condition
Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0; uses AND (&&) to require both conditions true. Predicate<int> isPositiveEven = x => x > 0 || x % 2 == 0; uses OR (||), allowing positives or evens, which is incorrect. Predicate<int> isPositiveEven = x => x < 0 && x % 2 == 0; checks for negative and even, wrong sign. Predicate<int> isPositiveEven = x => x % 2 == 0; checks only even, missing positivity.Final Answer:
Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0; -> Option DQuick Check:
Use AND to combine conditions for filtering [OK]
- Using || instead of && for combined conditions
- Checking wrong sign for positivity
- Ignoring one of the required conditions
Ready to prove it?
Pass the test and this pattern moves unknown
