Why is the C# compiler claiming 'use of an unassigned variable' prior to 'yield return' and dynamic?

微笑、不失礼 提交于 2019-12-10 13:56:55

问题


The compiler complains that resultingThing in the code below is being used before being assigned to.

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
        // ...
}

Why does it claim this?

I have tried a different version of the method in which there are no yield usages (e.g. just return IEnumerable<IThing>) but with the dynamic parameter, and I have tried a version of the method in which dynamic is not passed in (i.e. what we've done in prior versions of C#). These compile.


回答1:


I appears to be a compiler bug (or limitation, if you prefer).

I reduced the minimal failing case to:

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

Which gives the same compiler diagnostic, without involving member lookup on dynamics, nor iterator blocks.

For reference the mono compiler does not trip over that:

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)
    {

    }
}

Compiling that:

dmcs -v -warnaserror -warn:4 t.cs

No warnings



来源:https://stackoverflow.com/questions/6051711/why-is-the-c-sharp-compiler-claiming-use-of-an-unassigned-variable-prior-to-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!