<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx" -->

---
title: C# Tip: use yield return to return one item at the time
description: Explains the C# yield return keyword as a way to return collection items lazily, one at a time, instead of building an entire list upfront. Compares code and...
canonical: https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: C# Tip: use yield return to return one item at the time | daily.dev
og:description: Explains the C# yield return keyword as a way to return collection items lazily, one at a time, instead of building an entire list upfront. Compares code and...
og:url: https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx
og:image: https://api.daily.dev/og/posts/sCYtM2QMX.png
og:image:alt: C# Tip: use yield return to return one item at the time
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Tip: use yield return to return one item at the time

**[Code4IT](https://daily.dev/sources/code4it)** · 5 min read · 0 upvotes · 0 comments

## Summary

Explains the C# yield return keyword as a way to return collection items lazily, one at a time, instead of building an entire list upfront. Compares code and console output of a list-based approach versus a yield-based approach, highlighting performance and memory benefits. Notes that yield only works with IEnumerable/IEnumerator return types, and shows a real-world use case with NUnit's TestCaseSource attribute for generating test cases lazily.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.code4it.dev/csharptips/yield-return>

## Questions this post answers

### What is the difference between returning a List<T> and using yield return in C#?

Returning a List<T> builds the entire collection in memory before any item is used by the caller, while yield return produces items one at a time as the caller iterates, interleaving creation and consumption. This makes yield return faster and more memory-efficient when only part of a large collection is needed, since items are generated lazily rather than all at once.

_Developers choosing between eager and lazy collection patterns can explore C# yield return examples on daily.dev._

### Which return types support the yield return keyword in C#?

The yield keyword only works in methods returning IAsyncEnumerable<T>, IEnumerable<T>, IEnumerable, IEnumerator<T>, or IEnumerator. It cannot be used in a method that returns a concrete type like List<T>, since the compiler reports that such a type is not an iterator interface type.

_Anyone debugging iterator compile errors can dig into yield return constraints on daily.dev._

### How does NUnit use yield return for TestCaseSource test cases?

NUnit's TestCaseSource attribute points to a class implementing IEnumerable whose GetEnumerator method uses yield return to supply one test case object array at a time, rather than building a full list of cases upfront. This lets the test runner pull test cases lazily, as shown in NUnit's own documentation example with divide test cases.

_Developers writing data-driven NUnit tests can find yield-based TestCaseSource patterns on daily.dev._

## Similar posts on daily.dev

- [Demystifying async/await in C\#: The Hidden State Machine](https://daily.dev/posts/demystifying-async-await-in-c-the-hidden-state-machine-ej0npqym4) · C\# Corner · 0 upvotes · 0 comments
- [Looking at Unity finally made me understand the point of C\+\+ coroutines · Mathieu Ropert](https://daily.dev/posts/looking-at-unity-finally-made-me-understand-the-point-of-c-coroutines-mathieu-ropert-lyk7zdbtl) · Hacker News · 0 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"C# Tip: use yield return to return one item at the time","url":"https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx"},"datePublished":"2026-10-04T13:06:40.483Z","dateModified":"2026-10-04T13:19:49.647Z","description":"Explains the C# yield return keyword as a way to return collection items lazily, one at a time, instead of building an entire list upfront. Compares code and...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/6a00b2a25e315fb1b49ee141d45f46c4?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/6a00b2a25e315fb1b49ee141d45f46c4?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Code4IT","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Code4IT","logo":"https://media.daily.dev/image/upload/s--OaaG4G10--/f_auto,q_auto/v1791119049/logos/code4it?_a=BAMAMicg0","url":"https://daily.dev/sources/code4it"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Code4IT","item":"https://daily.dev/sources/code4it"},{"@type":"ListItem","position":3,"name":"C# Tip: use yield return to return one item at the time"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/c-tip-use-yield-return-to-return-one-item-at-the-time-scytm2qmx#faq","mainEntity":[{"@type":"Question","name":"What is the difference between returning a List<T> and using yield return in C#?","acceptedAnswer":{"@type":"Answer","text":"Returning a List<T> builds the entire collection in memory before any item is used by the caller, while yield return produces items one at a time as the caller iterates, interleaving creation and consumption. This makes yield return faster and more memory-efficient when only part of a large collection is needed, since items are generated lazily rather than all at once. Developers choosing between eager and lazy collection patterns can explore C# yield return examples on daily.dev."}},{"@type":"Question","name":"Which return types support the yield return keyword in C#?","acceptedAnswer":{"@type":"Answer","text":"The yield keyword only works in methods returning IAsyncEnumerable<T>, IEnumerable<T>, IEnumerable, IEnumerator<T>, or IEnumerator. It cannot be used in a method that returns a concrete type like List<T>, since the compiler reports that such a type is not an iterator interface type. Anyone debugging iterator compile errors can dig into yield return constraints on daily.dev."}},{"@type":"Question","name":"How does NUnit use yield return for TestCaseSource test cases?","acceptedAnswer":{"@type":"Answer","text":"NUnit's TestCaseSource attribute points to a class implementing IEnumerable whose GetEnumerator method uses yield return to supply one test case object array at a time, rather than building a full list of cases upfront. This lets the test runner pull test cases lazily, as shown in NUnit's own documentation example with divide test cases. Developers writing data-driven NUnit tests can find yield-based TestCaseSource patterns on daily.dev."}}]}
```

