Search code examples
githubmarkdowngithub-flavored-markdown

GitHub flavored markdown code syntax highlighting forcing type


Take this simple code markdown:

```cs
var x = new Locked<Rec>(new ("John", 35)); 
```

This renders in GitHub readme like this:

enter image description here

I now just add a definition:

```cs
record Rec(string Name, int Age);
var x= new Locked<Rec>(new ("John", 35)); 
```

and it now renders like this:

enter image description here

As you can see the type Locked and the new are for some reason no longer coloured purple. Why is this and how can I get around it ?


Solution

  • Visual Studio Code does the same thing.

    I'm not a C# developer, but the examples I found all instantiate the record inside Main:

    ```cs
    record Rec(string Name, int Age);
    
    public static void Main()
    {
        var x = new Locked<Rec>(new ("John", 35)); 
    }
    ```
    

    This seems to get both VSCode and GitHub to highlight syntax as you expect:

    GitHub rendering with syntax highlighting

    It works even better if you declare the record inside Main as well:

    GitHub rendering with syntax highlighting