Jan Paolo Go
Jan Paolo Go

Reputation: 6542

Why doesn't C# VS2017 intellisense work with var and local functions?

Why doesn't Intellisense work if I do something like this.

enter image description here

It does work though if I explicitly declare Foo

enter image description here

Upvotes: 0

Views: 111

Answers (2)

Julien Couvreur
Julien Couvreur

Reputation: 4983

The problem reproes. It is a problem with parsing incomplete code.

The line that you started (foo.) gets parsed together with the local function on the line below. This causes the local function not to be parsed correctly, so VS doesn't know that GetFoo is a local function any more or that it returns a Foo.

Upvotes: 2

Jonathon Chase
Jonathon Chase

Reputation: 9704

This is likely a bug in visual studio. As a work around, you can get the intellisense to play nice in this situation by declaring GetFoo at the top of your function's scope.

void Test()
{
    Foo GetFoo() => new Foo();

    var foo = GetFoo();
    foo.DoThing();

}

Intellisense Working

I observe the same behavior you have when placing the GetFoo declaration below the point in which I'm trying to use it, so it would seem that order matters.

Upvotes: 1

Related Questions