c# - 什么 C# 命名方案可用于符合 CLS 的属性和成员?

标签 c# cls-compliant

考虑以下不符合 CLS 的代码(仅在大小写上有所不同):

protected String username;

public String Username { get { return username;} set { username = value; } }

所以我把它改成了:

protected String _username;

public String Username { get { return _username;} set { _username = value; } }

这也不符合 CLS(有前导下划线)。

是否有任何不违反 cls 合规性的成员/属性的通用命名方案

最佳答案

与其公开支持字段,不如让您的属性成为虚拟属性,这样继承人就可以在不公开支持字段的实现的情况下覆盖功能。

private String username;

public virtual String Username { get { return username;} set { username = value; } }

继承不应该知道任何你的类实现。

参见 Best way to expose protected fields

关于c# - 什么 C# 命名方案可用于符合 CLS 的属性和成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282528/

相关文章:

c# - CS3016 - 在使用 Prism + MEF ExportModule 时我们如何解决这个问题?

c# - 如何使用 C# 禁用 CLS 合规性检查

c# - C# 和 VB 中的锯齿状数组 CLS 合规性不同?

c# - 引用名称大小写不符合 CLS

c# - WPF 中的静态资源

c# - 不同模型类的组合?

c# - 将值传递给动态加载的 Web 用户控件

c# - 基类型不符合 CLS,此警告的原因是什么?

c# - 通过反射设置变量

c# - 是否可以从 UWP 应用程序格式化/创建分区?