使用英语阅读

通过


编译器错误 CS8159

无法通过引用返回范围变量

示例

以下示例生成 CS8159:

// CS8159.cs (7,74)

using System.Linq;
class TestClass
{
    delegate ref char RefCharDelegate();
    void TestMethod()
    {
        var x = from c in "TestValue" select (RefCharDelegate)(() => ref c);
    }
}

更正此错误

若要返回范围变量,请重构为按值返回以更正此错误:

using System.Linq;
class TestClass
{
    delegate char RefCharDelegate();
    void TestMethod()
    {
        var x = from c in "TestValue" select (RefCharDelegate)(() => c);
    }
}