Search code examples
c#iteratorienumerableyield-return

Does yield return have any uses other than for IEnumerable?


Does C#'s yield return have a use outside of IEnumerables? For example, could it be used to implement coroutines without the use of multiple threads?


Solution

  • Your question

    Does C#'s yield return have a use outside of IEnumerables

    The answer is no, you can see this by the yield documentation

    yield (C# Reference)

    When you use the yield contextual keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, see IEnumerator for an example) when you implement the IEnumerable and IEnumerator pattern for a custom collection type.

    Iterator methods and get accessors

    The declaration of an iterator must meet the following requirements:

    >>>The return type must be IEnumerable, IEnumerable, IEnumerator, or IEnumerator <<<