C# - StyleCop - SA1121:UseBuiltInTypeAlias - 可读性规则
在 StyleCop 帮助手册、SO 和 Google 上没有找到它,所以在这里;)
在使用 StyleCop 期间,我收到一条警告:
SA1121 - 使用BuiltInTypeAlias - 可读性规则
该代码使用基本的 C# 之一 类型,但不使用内置的 类型的别名。
而不是使用类型名称或 完全限定类型名称, 这些类型的内置别名 应始终使用:bool、byte、 字符、十进制、双精度、短整型、整数、 长整型、对象、sbyte、浮点数、字符串、 ushort、uint、ulong。
所以 String.Empty
是错误的(取决于上述规则),而 string.Empty
是好的。
为什么使用内置别名更好?可以字符串。 Int32、
Int64
(等等)在特殊情况下会使代码中的某些内容变得复杂?
Not found it in StyleCop Help Manual, on SO and Google so here it is ;)
During StyleCop use I have a warning:
SA1121 - UseBuiltInTypeAlias -
Readability RulesThe code uses one of the basic C#
types, but does not use the built-in
alias for the type.Rather than using the type name or the
fully-qualified type name, the
built-in aliases for these types
should always be used: bool, byte,
char, decimal, double, short, int,
long, object, sbyte, float, string,
ushort, uint, ulong.
so String.Empty
is wrong (depend on above rules) and string.Empty
is good.
Why using built-in aliases is better? Can String. Int32
, Int64
(etc.) complicate something in the code on special scenarios?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只是澄清一下:并不是每个人都同意 StyleCop 作者的观点。 Win32 和 .NET 大师 Jeffrey Richter 在他的优秀著作 CLR via C# 中写道:
Just to clarify: not everyone agrees with the authors of StyleCop. Win32 and .NET guru Jeffrey Richter writes in his excellent book CLR via C#:
如果您有自己的
String
、Int32
等类型,这些类型最终可能会被用来代替System.*
-请不要这样做!最终这是个人喜好。我在任何地方都使用别名,但我知道有些人(例如 Jeffrey Richter)建议永远不要使用它们。保持一致可能是个好主意,仅此而已。如果您不喜欢 StyleCop 规则,请将其禁用。
请注意,方法等的名称应使用框架名称而不是别名,以便与语言无关。这对于私有/内部成员来说并不那么重要,但是您也可以对私有方法采用与公共方法相同的规则。
It would only really complicate the code if you had your own
String
,Int32
etc types which might end up being used instead ofSystem.*
- and please don't do that!Ultimately it's a personal preference. I use the aliases everywhere, but I know some people (e.g. Jeffrey Richter) advise never using them. It's probably a good idea to be consistent, that's all. If you don't like that StyleCop rule, disable it.
Note that names of methods etc should use the framework name rather than the alias, so as to be language-neutral. This isn't so important for private / internal members, but you might as well have the same rules for private methods as public ones.
因为内置别名是用该语言表达概念的更自然的方式。
有些文化说足球,有些文化说足球。哪一种更合适取决于上下文。
Because the built in alias is a more natural way to express the concept in that language.
Some cultures say soccer, others say football. Which one is more appropriate depends on the context.
此 StyleCop 规则假设使用别名可以减少所谓的“普通语言用户”的困惑。例如,它知道“long”类型,但不知何故害怕“System.Int64”类型,并且在看到它时感到困惑。就我个人而言,我认为保持代码风格一致很重要,不可能让所有人满意。
This StyleCop rule supposes that using aliases introduces less confusion to the so-believed-to-exist "average language user". Which knows, for example, 'long' type, but somehow scary of 'System.Int64' type and gets confused then sees it. Personally I think it's importatnt just to be consistent in your code style, it's impossible to satisfy everyone.
不那么混乱?对于我来说,对于传统上只是一个值的基本数据类型来说,包含静态函数似乎非常尴尬。我知道如果您只是存储一个值,则使用等效的基本数据类型,但是对于访问类的成员,在基本类型名称后面放置一个 .(点)似乎非常尴尬。
Less confusing? It seems very akward to me for a base data type, which traditionally is just a value, to include static functions. I understand using the base data type equivalent if you're just storing a value, but for accessing members of the class, it seems very akward to put a .(dot) after the base type name.
一个类比可能会有所帮助:字符串与System.String的关系就像步枪与步枪一样。 字符串是一种古老语言的遗物,是为老程序员提供的。 C# 没有“内置”数据类型,这些别名是为无法理解此概念的一代“C”程序员提供的。
An analogy might help: string is to System.String as musket is to rifle. string is a relic of an old language and is provided for old programmers. C# has no "built-in" datatypes and these aliases are provided for a generation of 'C' programmers who have trouble with this concept.