Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Predicate delegate type in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Predicate delegate type
Define Predicate delegate
↓
Create method matching Predicate signature
↓
Assign method to Predicate variable
↓
Call Predicate with input
↓
Return true or false
↓
Use result in condition or output
The Predicate delegate holds a method that takes one input and returns true or false. We assign a method to it, call it with a value, and get a boolean result.
Execution Sample
C Sharp (C#)
Predicate<int> isEven = x => x % 2 == 0;
bool result = isEven(4);
Console.WriteLine(result);
This code creates a Predicate to check if a number is even, tests it with 4, and prints the result.
Execution Table
StepActionInputPredicate Call ResultOutput
1Define Predicate isEven---
2Call isEven with 444 % 2 == 0 -> truetrue
3Print resulttrue-true
💡 Predicate call returns true for input 4, program ends after printing.
Variable Tracker
VariableStartAfter CallFinal
isEvenPredicate delegate assignedunchangedunchanged
resultundefinedtruetrue
Key Moments - 3 Insights
Why does the Predicate delegate only accept one input?
Predicate<T> is defined to take exactly one parameter of type T and return a bool, as shown in the execution_table step 2 where isEven takes one integer input.
What does the Predicate delegate return?
It always returns a boolean value (true or false), demonstrated in execution_table step 2 where the result is true.
Can we assign any method to a Predicate delegate?
Only methods that take one parameter of the correct type and return bool can be assigned, as shown in step 1 where the lambda matches Predicate<int> signature.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after calling isEven with 4?
Atrue
Bfalse
C4
Dundefined
💡 Hint
Check execution_table row 2 under 'Predicate Call Result' and 'Output'
At which step does the Predicate delegate get assigned a method?
AStep 3
BStep 1
CStep 2
DNo assignment happens
💡 Hint
Look at execution_table row 1 describing 'Define Predicate isEven'
If we call isEven with 5 instead of 4, what would change in the execution_table?
APredicate Call Result would be true
BOutput would be 5
CPredicate Call Result would be false
DNo change
💡 Hint
Recall that isEven returns true only if input % 2 == 0, see execution_table row 2
Concept Snapshot
Predicate<T> is a delegate type that takes one input of type T and returns a bool.
You assign a method or lambda matching this signature to it.
Calling the Predicate runs the method and returns true or false.
Commonly used for conditions and filtering.
Example: Predicate<int> isEven = x => x % 2 == 0;
Full Transcript
The Predicate delegate type in C# holds a method that takes one input and returns a boolean value. We first define a Predicate variable and assign it a method or lambda that matches its signature. Then, we call this Predicate with an input value. The method runs and returns true or false. For example, a Predicate<int> can check if a number is even by returning true when the number modulo 2 equals zero. The execution table shows defining the Predicate, calling it with 4, and printing the result true. Variables track the delegate assignment and the boolean result. Key points include that Predicate takes exactly one input and returns a bool, and only compatible methods can be assigned. The visual quiz tests understanding of the result value, assignment step, and behavior with different inputs.

Practice

(1/5)
1.

What does a Predicate<T> delegate represent in C#?

easy
A. A method that takes a parameter of type T and returns a boolean indicating a condition.
B. A method that takes no parameters and returns void.
C. A method that takes two parameters and returns an integer.
D. A method that returns a string representation of an object.

Solution

  1. Step 1: Understand the Predicate delegate signature

    A Predicate delegate always takes one parameter of type T and returns a boolean value.
  2. 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.
  3. Final Answer:

    A method that takes a parameter of type T and returns a boolean indicating a condition. -> Option A
  4. Quick Check:

    Predicate<T> = method(T) returns bool [OK]
Hint: Predicate always returns true or false for a given input [OK]
Common Mistakes:
  • Confusing Predicate with Action or Func delegates
  • Thinking Predicate returns void or other types
  • Assuming Predicate takes multiple parameters
2.

Which of the following is the correct syntax to declare a Predicate delegate for integers?

?
easy
A. Predicate<int> isPositive = x => x > 0;
B. Predicate<int> isPositive = (int x) => { return x; };
C. Predicate<int> isPositive = x => x + 1;
D. Predicate<int> isPositive = () => true;

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Predicate<int> isPositive = x => x > 0; -> Option A
  4. Quick Check:

    Predicate<int> = int input => bool output [OK]
Hint: Predicate must return bool, not int or void [OK]
Common Mistakes:
  • Returning int instead of bool
  • Missing parameter in lambda
  • Using incorrect lambda syntax
3.

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);
    }
}
medium
A. 3
B. 2
C. 1
D. 0

Solution

  1. Step 1: Understand the Predicate condition

    The predicate checks if the string length is greater than 5.
  2. Step 2: Count words with length > 5

    "apple" length 5 (false), "banana" length 6 (true), "pear" length 4 (false). Only "banana" passes.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Words with length > 5 are "banana" only, count = 1 [Incorrect]
Hint: Count items where predicate returns true [OK]
Common Mistakes:
  • Counting words with length equal to 5
  • Misreading predicate condition
  • Assuming all words pass the test
4.

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);
medium
A. The Predicate delegate is not assigned correctly.
B. The foreach loop variable should be declared as var.
C. The if condition should call the delegate with an argument.
D. The Console.WriteLine statement is missing parentheses.

Solution

  1. Step 1: Check how Predicate is used in if condition

    The code uses if (isEven) which checks the delegate itself, not its result.
  2. Step 2: Correct usage requires calling the delegate with an argument

    It should be if (isEven(num)) to evaluate the condition for each number.
  3. Final Answer:

    The if condition should call the delegate with an argument. -> Option C
  4. Quick Check:

    Call Predicate with input: isEven(num) [OK]
Hint: Always call Predicate with an argument inside if [OK]
Common Mistakes:
  • Using delegate name without parentheses
  • Confusing delegate variable with method call
  • Ignoring parameter requirement
5.

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?

hard
A. Predicate<int> isPositiveEven = x => x % 2 == 0;
B. Predicate<int> isPositiveEven = x => x > 0 || x % 2 == 0;
C. Predicate<int> isPositiveEven = x => x < 0 && x % 2 == 0;
D. Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0;

Solution

  1. Step 1: Understand the filtering requirement

    We want numbers that are both positive and even.
  2. 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.
  3. Final Answer:

    Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0; -> Option D
  4. Quick Check:

    Use AND to combine conditions for filtering [OK]
Hint: Use && to combine multiple conditions in Predicate [OK]
Common Mistakes:
  • Using || instead of && for combined conditions
  • Checking wrong sign for positivity
  • Ignoring one of the required conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard