Search code examples
c#visual-studioresharpersyntax-highlighting

Syntax highlighting not working in Visual Studio


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:

  • When hovering on the file: "An expression is too long or complex to compile"
  • Within the file: "Argument is 'ref' while parameter is declared as 'value'"

An expression is too long or complex to compile

Argument is ref while parameter is declared as value

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);

        // ...
    }
}

Solution

  • 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!