this post was submitted on 01 Aug 2025
5 points (85.7% liked)
C Sharp
1703 readers
1 users here now
A community about the C# programming language
Getting started
Useful resources
- C# documentation
- C# Language Reference
- C# Programming Guide
- C# Coding Conventions
- .NET Framework Reference Source Code
IDEs and code editors
- Visual Studio (Windows/Mac)
- Rider (Windows/Mac/Linux)
- Visual Studio Code (Windows/Mac/Linux)
Tools
Rules
- Rule 1: Follow Lemmy rules
- Rule 2: Be excellent to each other, no hostility towards users for any reason
- Rule 3: No spam of tools/companies/advertisements
Related communities
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Second one was, IIRC, one of the primary motivating use cases for
IAsyncEnumerable<T>
. It has to beIAsyncEnumerable<T>
all the way up and down, but it's elegant enough. Quite often, depending on the API, it might naturally be implemented by a variant of "paging" behind the scenes. One advantage here is that since the updates throughout the finalawait foreach
can happen entirely on the UI thread (assuming a compliantSynchronizationContext.Current
, which is the case at least for WPF), you can see the results streaming in one-by-one as they arrive from the original repository if you want.Before watching the video, from the title, I correctly guessed what the first bug would be, but my guess for what bug #2 would be was one that's more related to the far more insidious one that he describes starting at 17:27. You don't have to get very fancy to see connections held open too long:
I don't know how common it is more broadly, but I've seen plenty of code that overlooks
using
and just directly callsDispose
(or, more commonly, aClose
method). While it's generally advised to favorusing
over directly callingDispose
, admittedly it's not always a huge deal: if you don't otherwise have a robust story for what happens when exceptions get thrown, then it doesn't ordinarily matter that you're not explicitly cleaning up those resources, since the program might just be ending anyway.With iterators, this can leave the connection open even if no exception is thrown.
try
/finally
blocks (includingusing
scopes, which I assume still get lowered to the same) get extra treatment to make sure that thefinally
part gets executed when the iterator is disposed, which happens at the end of any foreach loop over it (including the implicit loop behind calls like.First
).