Any
needs a function (Func
) that (and I'm assuming that stringArray
is just a string)
- takes a
char
input
- returns a
bool
.
The most common way to use this is by making an inline anonymous function (a lambda expression). However, you can pass an actual function instead. The system will look through the arguments and return type to make sure it matches up, just like a lambda statement.
It just so happens that the function Contains
takes a char
as an input and returns a bool
. That's why you can pass the name of the function instead of of calling it (using parentheses with an argument list).
Take for example if we made our own function:
public static bool Contains2 (this string input, char c)
{
//....
}
stringArray.Any(stringToCheck.Contains2)
This would also fit the bill. Notice how there are no ()
parentheses on Contains2. This is because we are passing the function itself.
Your lambda also works because it takes a char input (s =>
) and returns a bool (the result of Contains(s)
).