Returning large datasets blocks memory.
Streaming them changes the entire performance profile.
public async IAsyncEnumerable<int> StreamNumbers()
{
for (int i = 0; i < 1000; i++)
{
await Task.Delay(10);
yield return i;
}
}
Why it works
-
Consumers process data as it arrives
-
Lower memory footprint
-
Faster perceived response times
Perfect for:
-
Reporting APIs
-
Log streaming
-
Long-running queries
