Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Lambda expression with List.ConvertAll

Tags:

c#

lambda

I am pretty new to C# and as a practice exercise was trying to convert a console input of Y's and N's into boolean array of true and false (Y=true N=false).

enter image description here

I get the "Only assignment call, increment, decrement, await and new object expressions can be used as a statement" error. Any suggestions?

like image 732
Hari Avatar asked Oct 29 '25 15:10

Hari


1 Answers

Try:

bool[] tempArray = Console.ReadLine().ToList().ConvertAll(ch => Char.Equals(ch, 'Y')).ToArray();

Lambdas do not need the type of their parameters to be specified in the declaration, they are inferred automatically. You would also need to convert to the list of bools to an array with ToArray.

like image 83
Stephen Hewlett Avatar answered Oct 31 '25 05:10

Stephen Hewlett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!