为什么C#编译器在'yield return'和动态之前声称'使用未赋值的变量'?

Kit*_*Kit 5 dynamic yield-keyword out-parameters c#-4.0

编译器抱怨resultingThing在下面的代码中被分配之前正在使用.

private IEnumerable<IThing> FindThings(dynamic spec)
{
    if (spec == null)
        yield break;

    IThing resultingThing;
    if (spec.Something > 0 && dictionary.TryGetValue(spec.Something, out resultingThing))
        yield return resultingThing;
    else
        // ...
}
Run Code Online (Sandbox Code Playgroud)

为什么声称这个?

我已经尝试了一个不同版本的方法,其中没有产量使用(例如,只是return IEnumerable<IThing>)但是使用动态参数,我尝试了一种不传递动态的方法版本(即我们在以前版本的C#).这些编译.

seh*_*ehe 1

我似乎是一个编译器错误(或限制,如果您愿意的话)。

我将最小失败案例减少为:

static private IThing FindThings(dynamic spec)
{
    IThing resultingThing;
    if ((null!=spec) && dictionary.TryGetValue(spec, out resultingThing))
        return resultingThing;
return null;
}
Run Code Online (Sandbox Code Playgroud)

它提供了相同的编译器诊断,不涉及动态成员查找,也不涉及迭代器块。

作为参考,mono 编译器不会出错

using System;
using System.Collections.Generic;

public static class X
{
    public interface IThing { }

    private static readonly IDictionary<string, IThing> dictionary = new Dictionary<string, IThing>();

    static private IThing FindThings(dynamic spec)
    {
        IThing resultingThing;
        if ((null!=spec) && dictionary.TryGetValue(spec, out resultingThing))
            return resultingThing;
        return null;
    }

    public static void Main(string[] s)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

编译:

dmcs -v -warnaserror -warn:4 t.cs
Run Code Online (Sandbox Code Playgroud)

无警告