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?
ForEach(which is a List method, not an IEnumerable method), I recommend (and use religiously)foreach (var elm in enumerable) { ... }to indicate a side-effect process. As a bonus, there is no type inference error. Hint about the error: what is the return type ofWriteLine? - user166390IEnumerable<string> hardToMaintain() { return new [] {"a","b","c"}.Select(e => {Console.WriteLine("" + e); return "dummy"}; }; ... for(var i in hardToMaintain()) { Console.Writeline("bar"); }What is the Output?ForEachbeing a List method, and not a (lazy) IEnumerable method "fixes" this by forcing the evaluation. However, it doesn't fix the fundamental problem, IMOHO. However, I find useforeach...to indicate that the code has (or may have) side-effects. - user166390IEnumerable.ForEachorIEnumerable.Select(Action):-) - user166390