vasa.dhananjay
vasa.dhananjay

Reputation: 165

Operator '!' cannot be applied to operand of type 'method group'

Probably a duplicate but of the related questions, I've yet to find a solution that works. Trying to count the number of digits up to a a number of character in a string.

Getting the error: '!' cannot be applied to operand of type 'method group'

line.TakeWhile(!Char.IsLetterOrDigit).Count())

Upvotes: 1

Views: 2095

Answers (2)

Random Dev
Random Dev

Reputation: 52300

the problem is exactly what the error tells you: you cannot use ! on an function (the Char.IsLetterOrDigit) - one simple solution is expanding it into a lambda:

line.TakeWhile(c => !Char.IsLetterOrDigit(c)).Count())

Upvotes: 2

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

You need to use lambda expression instead of method group syntax here:

line.TakeWhile(x => !Char.IsLetterOrDigit(x)).Count())

Upvotes: 2

Related Questions