I wrote code like this:
System.IO.File.ReadAllLines("c://test.txt")
.Select(val => Console.WriteLine(val)
);
And I’m getting a compiler error:
The type arguments for method
‘System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,
System.Func)’ cannot be inferred from the usage.
Try specifying the type arguments explicitly.
WriteLine can accept string and object as the parameter, that’s probably a source of the error but how do I rewrite it?
This answer includes summary of other answers.
There isn’t any problem with parameter type, problem is that Select method must return a value and it shouldn’t be used in this case. ForEach would be the correct method, but only List has it, not every IEnumerable. So:
will do the trick (while incurring some overhead by converting to a list).
There is one more way: static Array.ForEach method. So my code will look like this: