You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中为C#的接口、using及变量/属性设置不同颜色?

解决VS Code中C#语法高亮自定义问题

要在VS Code里给C#的接口、using语句、局部变量/类属性设置不同高亮颜色,需通过自定义settings.json的语法高亮规则实现,步骤如下:

  • 进入设置编辑模式:按下Ctrl+,(Windows/Linux)或Cmd+,(Mac)打开设置面板,点击右上角的{}图标,进入settings.json直接编辑。
  • 添加自定义高亮规则:在配置中插入editor.tokenColorCustomizations节点,针对不同元素设置颜色。示例配置如下:
    "editor.tokenColorCustomizations": {
      "textMateRules": [
        // 自定义using语句颜色
        {
          "scope": "keyword.control.using.cs",
          "settings": {
            "foreground": "#0066cc"
          }
        },
        // 自定义接口颜色
        {
          "scope": "entity.name.type.interface.cs",
          "settings": {
            "foreground": "#7744aa"
          }
        },
        // 自定义局部变量颜色
        {
          "scope": "variable.other.local.cs",
          "settings": {
            "foreground": "#ff6600"
          }
        },
        // 自定义类属性颜色
        {
          "scope": "variable.other.property.cs",
          "settings": {
            "foreground": "#009900"
          }
        }
      ]
    }
    
  • 获取准确的元素Scope值:如果不清楚目标元素对应的Scope,按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),执行Developer: Inspect Editor Tokens and Scopes命令,鼠标悬停在要自定义的代码元素上,就能查看对应的Scope名称,替换到上述配置中即可。

当前配色方案:
当前配色方案

期望达到的效果:
期望效果

参考的修改后设置界面:
修改后的设置界面

内容的提问来源于stack exchange,提问作者Soerx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 10:22:23