Skip to content

Unexpected error CS8200 for an expression variable declared within a lambda within a field initializer. #16935

@AlekseyTs

Description

@AlekseyTs
        [Fact]
        public void GlobalCode_Lambda_02()
        {
            var source =
@"
System.Func<bool> l = () => TakeOutParam(1, out int x1) && Dummy(x1); 
System.Console.WriteLine(l());

static bool Dummy(int x) 
{
    System.Console.WriteLine(x);
    return true;
}

static bool TakeOutParam(int y, out int x) 
{
    x = y;
    return true;
}
";
            var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script);
            CompileAndVerify(compilation, expectedOutput: @"1
True");

            var tree = compilation.SyntaxTrees.Single();
            var model = compilation.GetSemanticModel(tree);

            var x1Decl = GetOutVarDeclarations(tree, "x1").Single();
            var x1Ref = GetReferences(tree, "x1").Single();
            VerifyModelForOutVar(model, x1Decl, x1Ref);
        }

Result:

(2,49): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions