What’s the use of yield break? [duplicate]

To give a code example, say you want to write an iterator that returns nothing if the source is null or empty.

public IEnumerable<T> EnumerateThroughNull<T>(IEnumerable<T> source)
{
    if (source == null)
        yield break;

    foreach (T item in source)
        yield return item;
}

Without the yield break it becomes impossible to return an empty set inside an iterator.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)