为什么C#编译器声称在“yield return”和动态之前使用未分配的变量? [英] Why is the C# compiler claiming 'use of an unassigned variable' prior to 'yield return' and dynamic?

查看:154
本文介绍了为什么C#编译器声称在“yield return”和动态之前使用未分配的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器会在分配之前使用下面的代码中的 resultingThing

  private IEnumerable< IThing> FindThings(动态规范)
{
if(spec == null)
yield break;


if(spec.Something> 0&&&&&& amp;&
else
// ...
}

我试过一个不同版本的方法,其中没有 yield 的用法(例如只是 return IEnumerable< IThing> ),但使用了动态参数,并且我尝试了不传递动态 (即我们在以前的C#版本中所做的)。这些编译。

解决方案

我似乎是一个编译器错误(或限制,如果你喜欢)
$ b

我将最小失败的情况减少到:

  static private IThing FindThings b $ b {
结果Thing;
if((null!= spec)&&&& dictionary.TryGetValue(spec,out resultThing))
return resultThing;
return null;
}

这给出了相同的编译器诊断,


$ b



$ p> 使用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(动态规格)
{
IThing resultThing;
if((null!= spec)&&&& dictionary.TryGetValue(spec,out resultThing))
return resultThing;
return null;
}

public static void Main(string [] s)
{

}
}

编译:

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

无警告


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.

解决方案

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

这篇关于为什么C#编译器声称在“yield return”和动态之前使用未分配的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆