I just saw the following answer: Is there a better way to create acronym from upper letters in C#? and it has the following code:
string.Join("", s.Where(char.IsUpper));
How does the char.IsUpper work here? (Instead of x => char.IsUpper(x))
I just saw the following answer: Is there a better way to create acronym from upper letters in C#? and it has the following code:
string.Join("", s.Where(char.IsUpper));
How does the char.IsUpper work here? (Instead of x => char.IsUpper(x))
char.IsUpper is a method group, which itself takes a char and returns a bool, so it's a valid predicate for use with Where().
The code is referencing the method by name in much the same way as you would with any other method when specifying a delegate, instead of directly invoking it, so the parentheses aren't necessary.
The parentheses are necessary if you're wrapping it in a lambda expression x => char.IsUpper(x), because you're calling the method and returning the result, within the expression.
char.IsUpper refers to a method group which is passed to the Where function as a typed delegate via an implicit conversion which you can read in the Covariance and Contravariance in C#, Part Three: Method Group Conversion Variance article by Eric Lippert.
I believe char.IsUpper (without parentheses) evaluates to a reference to the method, that can be passed as a predicate. If you added parentheses, that would just immediately invoke the method and attempt to pass the result instead of passing the method itself.
Where<char> takes a Func<char, bool> as a parameter. By using x => char.isUpper(x), you are creating a new Func to be used by Where. However, the toUpper method, takes a char, and returns a bool. Therefore, it can be used directly as the parameter for Where.