When code builds but looks invalid, and intellisense is dead
I recently encountered what I at first thought was a bug in Visual Studio, but which turned out to be a poorly described configuration error on my part.
The problem manifests itself like this:
Notice the red color – that is code that Visual Studio can not identify and provide intellisense for. This is valid code that builds and runs correctly, but which still appears invalid in the editor. There is no error message, and no obvious reason for this. Furthermore you can navigate to the class either by clicking F12, or by right clicking it and choosing go to declaration, yet Visual Studio still looks as if unable to identify the class and provide any coding help for it.
Explanation
The explanation is quite simple, even if it was not initially obvious:
When I opened the .csproj file for my project, I found something like the above: An xml node <PackageReference ... />
and a node <ProjectReference ... />
that included the same project from two different sources.
My main project referenced a secondary project as a nuget package, but that secondary project was also available in the same VS Solution. At some point I had probably included it as a project reference by accident, which apparently resulted in Visual Studio getting confused.
Solution
Easy fix: Delete the project reference. In the example above, I just removed the whole <ItemGroup>
section from my project file. Result: Everything immediately works the way it should: