Ready.
In order to complete our generic delegates, we will now talk about the predicate.
We have talked about the action the func right here.
And now we'll talk about the predicate.
So the predicate basically just returns boolean.
You can see that here.
It's a delegate bool.
So we basically do that to match any specific conditions.
Uh I haven't seen that getting used a lot, but I've seen it in some cases.
So this is why it's part of this course.
For example, if you want to check if, let's say a person has a specific age or if a product matches
a specific category or whatsoever, right.
So predicate and this time it's the input.
So parameter int.
For example we would simply want to check if a number is even or odd.
We're doing that by taking the modulo operator and well modulo two.
Right.
So if the value is zero then it's even if it's not zero it's odd.
That's this is basically just how you check if a number is even or odd.
So predicate let's say um is even.
So we name it kind of like a bool, right.
Because it returns a bool and we say goes to we say x right here.
We can also write that down in a single line.
But I want to do it like this.
We simply return if our x modulo two is equals to zero.
In that case it is.
Even if it's not zero, then it's not even great.
Now we can simply go to our console right line.
We are getting a return value, right?
Not like an action.
As you can see, we get our bool.
So we will get a true or false.
So we can simply say is even here and write down um our number let's say uh five.
So in that case it will not be even.
So we should see a false.
Just give it a second to compile.
There we go.
False.
Right.
And if I write down four it will work out fine.
Great.
So you know, that one was quite easy.
As I said, I just don't want you to run into situations where you will see something like that and
you are like, what the heck is the predicate, right?
This is why we are adding that here.
You know, actions and functions, they are more commonly used.
So functions and actions, chances are you will run into them more often, right?
But as I said, in order to complete this section on all the generic delegates, I wanted to also add
the predicate.
So in comparison, we now have an action.
An action does not return anything, it just executes code.
We have a function where we can specify tons of parameters and the return value that we will get back
once the code got executed, just like a method.
And then we have a predicate which is basically just returning true or false.
So boolean value.
And one great example on how you would use the predicate for example, is if you would create a list
of integers.
For example, let's call it int equals to new list.
Let's just add some of them to to the list.
Right?
Let's say we have one, two, three, four, five, six, seven and eight like that.
We would have now our int list.
And we can now use our predicate here to simply check if or to to get all the values that are inside
of our int list, which are even.
So we could say var even int equals to.
And now we take our ints our list.
And then we can call our predicate on that list.
So we simply say find all.
Now take a look here at the Find all.
Let's hover above that.
It takes the predicate right.
This is just one example of where you would use that.
But here we take a predicate.
So we can write down a seven.
Because the seven is a predicate.
It simply takes x and checks if x modulo two is zero.
So is it even or odd.
And then if we just set a breakpoint here and start the application, you'll be able to see inside of
our uh, even ins list that we have here, we will have four values two, four, six, and eight.
And as I said, it's not about that list.
It's not about whatever.
It's just about that.
You can see that there are methods commonly used in generic types, such as dictionaries and lists,
which are generic, right, that make use of, for example, predicates.
Uh, just like in that scenario where you will have kind of like a filter or matching functionality
that's commonly used there.
Alrighty.
And that's it for the generic predicate.
And see.