Visual Studio Professional 2022, ReSharper 2022.
When writing some "normal" C# code in Visual Studio I hit a problem where all syntax highlighting was lost, with some unusual compiler errors. Syntax highlighting continues to work as expected in all other files.
The compiler errors show differently in different locations:
As seen in the second image, the code causing the error is the following:
public class Location : ILocation
{
public int InventoryId { get; }
public IReferences References { get; }
public Location(IInventoryLocation location)
{
var shortRef = location.References
.Single(ref => ref.ReferenceType == ReferenceType.Short);
// ...
}
}
I noticed the problem but posted here in case it helps others. I was using ref
as the name of the lambda variable, which is of course a keyword in C#. Changing this to reference
or anything else restores the syntax highlighting. Of course the compiler error within the file makes it quite clear but it wasn't the error being presented to me initially. I tried a few other keywords, and only ref
seems to break the syntax highlighting. StackOverflow's syntax highlighting is in fact better at revealing the issue!