TGViewer
Programming & AI Tips 💡 Programming & AI Tips 💡 @programmingtip · 46.3K subscribers
Post #231 1.1K
No Need for Temporary Collections, Yield Might Help ✅

Normally when we need to fetch the items from a collection we might create a Temporary List to hold the retrieved items and return. 🌀

Following is the C# code using the temporary list :

public List<int> GetValuesGreaterThan100 
(List<int> masterCollection)
{
List<int> tempResult = new List<int>();
foreach (var value in masterCollection)
{
if (value > 100)
tempResult.Add(value);
}
return tempResult;
}


➖➖➖➖➖➖➖

To avoid the usage of this temporary collection you could choose to use the yield. 🔥

It will yield the result as and when the result set is enumerated.

Below is the code using the yield keyword :

public IEnumerable<int> GetValuesGreaterThan100(List<int> masterCollection)
{
foreach (var value in masterCollection)
{
if (value > 100)
yield return value;
}
}

〰〰〰〰〰〰
#CSharp #Collections
@ProgrammingTip
More from @programmingtip
  1. Oct 7, 2026OpenAI Decisions API is in public beta 🚀 OpenAI's Decisions API is now in public beta. It…
  2. Oct 6, 2026Cloudflare shipped a Web Search API ⚡️ Cloudflare introduced a Web Search API. Search can…
  3. Oct 5, 2026AI is changing the developer career ladder 🚀 GitHub's latest developer career advice is r…
  4. Oct 2, 2026GPT-6 Astra gets an Ultrafast API tier 🚀 OpenAI added an Ultrafast speed tier for GPT-6 A…
  5. Oct 1, 2026Cloudflare shipped an agentic CLI for its API ⚡️ Cloudflare launched cf, an agentic CLI fo…
  6. Sep 30, 2026OpenAI introduced Dots, always-on agents 🚀 OpenAI introduced Dots, its take on always-on…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →