0

I'm still getting acquainted with delegates and lambdas, I'm not using LINQ, and I also only discovered the ConvertAll function today, so I'm experimenting and asking this to bolster my understanding.

The task I had was to establish whether a string of numbers were even or odd. So first, convert the string list to an int list, and from there into a bool list. As bloated as the code would be, I wondered if I could get it all on one line and reduce the need for an extra for loop.

string numbers = "2 4 7 8 10";

List<bool> evenBools = new List<bool>(Array.ConvertAll(numbers.Split(' '), (x = Convert.Int32) => x % 2 == 0))

The expected result is [true, true, false, true, true]. Obviously the code doesn't work.

I understand that the second argument of Array.ConvertAll) requires the conversation to take place. From string to int, that's simply Convert.ToInt32. Is it possible to do that on the fly though (i.e. on the left side of the lambda expression), so that I can get on with the bool conversion and return on the right?

2
  • " (ie. on the left side of the lambda expression)" - the left hand side of => is only ever a list of parameters, possibly in parentheses, possibly using implicit typing. It's not clear what you expected x = Convert.Int32 to mean there, but you should reset your expectations of what can appear to the left hand side of =>. Commented May 27, 2020 at 11:19
  • After Linq has come, there is no need to use Array.ConvertAll<> in you case (but it is possible). You could do var evenBools = numbers.Split(' ').Select(x => Convert.ToInt32(x) % 2 == 0).ToList(); Commented May 27, 2020 at 13:15

2 Answers 2

4

Second parameter of ConvertAll method is Converter<TInput,TOutput> delegate. You can use anonymous method here, pass string input to it (left side of =>), parse to int inside and return bool value, indicating whether it's even or odd

var boolArray = Array.ConvertAll(numbers.Split(' '), input => Convert.ToInt32(input) % 2 == 0);
List<bool> evenBools = new List<bool>(boolArray);

Finally create a List<bool> from array.

Left side of lambda expressions contains input parameters only (input string in your case). Right side is used for expressions and statements, here you have a logic for parsing and return a bool value.

With help of System.Linq it can be written on the similar way

var boolArray = numbers.Split(' ').Select(input => Convert.ToInt32(input) % 2 == 0).ToArray();

You also might use int.Parse instead of Convert.ToInt32

Sign up to request clarification or add additional context in comments.

1 Comment

@TomPengelly Does it answer your question, do you need more details or information?
0
var boolList = numbers.Split(' ').Select(Int32.Parse).ToList().ConvertAll(i => i % 2 == 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.