Skip to content

Instantly share code, notes, and snippets.

@idg10
Last active September 19, 2018 06:14
Show Gist options
  • Save idg10/28e013f4480cab2557d8521ada884027 to your computer and use it in GitHub Desktop.
Save idg10/28e013f4480cab2557d8521ada884027 to your computer and use it in GitHub Desktop.
Incorrect resource handling with combined enumerable and Task-based deferred execution
// 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