Search code examples
c#linq-to-entitiesreverse-engineeringdnspy

Duplicated yield break in Enumerable


I am learning linq throught decompiling the System.Core.dll (4.7.3429.0) by dnSpy and see duplicated 'yield break;' in several cases, like this one

private static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source)
    {
        foreach (object obj in source)
        {
            yield return (TResult)((object)obj);
        }
        IEnumerator enumerator = null;
        yield break;
        yield break;
    }

Will the second one never be called?

What is it purpose? Does it a bag in dnSpy or missusing in .NET?


Solution

  • Yep, bug. Both reference source and Telerik's JustDecompile simply show

    foreach (object obj in source) yield return (TResult)obj;
    

    (JustDecompile adds braces).

    Both yield break statements are redundant. And IEnumerator enumerator = null; is a redundant statement as well.