如何配置EditorConfig以允许使用全下划线命名的变量?
当然没问题!EditorConfig完全能帮你配置规则,允许使用像__、___这类全下划线命名的变量,下面我就针对你示例里的C#场景,一步步教你怎么设置:
配置EditorConfig允许全下划线变量
首先,找到项目根目录下的.editorconfig文件(如果没有就新建一个),添加对应的命名规则即可。
针对C#的具体配置
在.editorconfig中插入以下配置代码:
# 规则:允许全下划线命名的标识符 dotnet_naming_rule.allow_all_underscores.severity = suggestion dotnet_naming_rule.allow_all_underscores.symbols = all_underscore_identifiers dotnet_naming_rule.allow_all_underscores.style = allow_all_underscores_style # 定义匹配范围:适用于参数和局部变量 dotnet_naming_symbols.all_underscore_identifiers.applicable_kinds = parameter, local dotnet_naming_symbols.all_underscore_identifiers.applicable_accessibilities = * # 定义允许的命名风格:无大小写要求,仅下划线 dotnet_naming_style.allow_all_underscores_style.required_prefix = dotnet_naming_style.allow_all_underscores_style.required_suffix = dotnet_naming_style.allow_all_underscores_style.word_separator = _ dotnet_naming_style.allow_all_underscores_style.capitalization = none
配置说明
applicable_kinds = parameter, local:专门针对Lambda参数(比如你示例里的_和__)、局部变量生效,不会影响类成员、方法名等其他标识符。capitalization = none:关闭大小写格式校验,允许纯下划线的命名形式。- 如果是JavaScript/TypeScript这类语言,你可以用更简单的配置:
# JS/TS 允许全下划线变量 identifier_case = unset no_unused_parameters = false
生效方式
配置完成后,重启你的编辑器(比如Visual Studio、VS Code),再写Changed += (_, __) => valueChanged?.Invoke(Value);这类代码时,编辑器就不会把全下划线的变量标记为命名违规了。
内容的提问来源于stack exchange,提问作者C. Groot.
相关产品推荐
相关产品推荐

