Last active
September 19, 2018 06:14
-
-
Save idg10/28e013f4480cab2557d8521ada884027 to your computer and use it in GitHub Desktop.
Incorrect resource handling with combined enumerable and Task-based deferred execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This won't work | |
public static IEnumerable<Task<int>> ParseFile(string path) | |
{ | |
using (var reader = new StreamReader(path)) | |
{ | |
while (!reader.EndOfStream) | |
{ | |
yield return ReadAndParseAsync(reader); | |
} | |
} | |
} | |
private static async Task<int> ReadAndParseAsync(StreamReader reader) | |
{ | |
string line = await reader.ReadLineAsync(); | |
return int.Parse(line); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment