在C#中,使用.editorConfig,是否有任何方法可以强迫开发人员声明每个参数是无效的还是不允许的?
在c#中,我可以在方法的签名中表达一个参数,例如:
private void doSomething(string? value)
同样,我可以表达一个参数必须不如:
private void doSomething(string value!!)
是否存在任何规则,或者有任何方法我可以创建一个规则 - 对于.editorconfig。哪个将迫使项目上的开发人员始终在方法的签名中声明每个输入参数是否允许null?
如果是这样,我该怎么做?
In C#, I can express a parameter is nullable in a method's signature like:
private void doSomething(string? value)
Similarly, I can express a parameter must not be null like:
private void doSomething(string value!!)
Does any rule exist -- or is there any way I can create a rule -- for .editorconfig which will force developers on a project to always declare in a method's signature whether each input parameter allows null?
If so, how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑会制定这样的规则。 null参数检查!更适合公共API作为保障。如果内部呼叫堆栈很长,则进行无效检查会使可能不需要的性能压力。
坚持无效的参考类型在其他任何地方都更合适,因为它对运行时没有影响。启用了无效的参考类型,一个值都没有声明?或者 !!暗示不支持零,并将导致相关警告。
I doubt such a rule will be made. Null parameter checking with !! is better suited for public APIs as a safeguard. If there are long internal call stacks, applying null checks puts a strain on performance that may not be necessary.
Sticking to nullable reference types everywhere else is more appropriate because it has no impact on performance at runtime. With nullable reference types enabled, a value declared with neither ? or !! is implied to not support null and will lead to the relevant warnings.