一、C# 10中命令空间(namespace)定义
1、之前的写法
namespace SampleCode { public class MyClass { } }
2、新的写法
namespace SampleCode; public class MyClass { }
二、命名空间定义的警告配置
vs2019和vs2022都支持在项目中加入一个.editorconfig的文件来统一处理代码的样式风格,命名风格等问题,这个配置文件写在sln解决方案启动文件同级别目录下
1、需要强制使用C# 10之前的写法,如果不是这么写的,就报错,编译失败
在.editorconfig中改成配置如下:
# IDE0160: Convert to block-scoped namespace csharp_style_namespace_declarations = block_scoped:warning //给警告 或者 csharp_style_namespace_declarations = block_scoped:error //直接编译失败
2、需要强制使用C# 10的新写法
在.editorconfig中改成配置如下:
# IDE0160: Convert to file-scoped namespace csharp_style_namespace_declarations = file_scoped:warning
三、Jetbrain Rider中的配置
参考文档:https://www.jetbrains.com/help/rider/Using_EditorConfig.html
1、.editorconfig中配置
csharp_style_namespace_declarations = file_scoped dotnet_diagnostic.IDE0161.severity = error
2、CSProj项目文件的中配置
<PropertyGroup> <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> </PropertyGroup>
留下您的脚步
最近评论