有什么理由不在系统命名空间中使用别名吗?

发布于 2024-07-19 18:55:29 字数 178 浏览 6 评论 0原文

我一直习惯使用 String 代替 string,使用 Int32 代替 int。 主要是因为一切都是一个类,所以我喜欢保持一致并像对待所有其他类/对象一样对待所谓的“基元”。

我刚刚看到一篇关于编码标准的文章,其中提到“始终使用 C# 预定义类型而不是系统命名空间中的别名”。

但它没有说明原因。

I've always been used to using String instead of string and Int32 instead of int. Mainly because everything is a class, so I like to keep consistent and treat so called 'primitives' just like every other class/object.

I just saw an article on coding standards where it mentioned "Always use C# predefined types rather than the aliases in the System Namespace."

It didn't say why though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空名 2024-07-26 18:55:29

这些类型是完全可以互换的,并且可以编译为相同的 IL。 您的编码标准中应该有一条关键规则 - 如果您正在编辑共享文件,请使用与现有代码相同的样式。 没有什么比试图修复一个样式改变了所有其他功能的文件更烦人的了。

所以——选择一个并保持一致。

The types are completely interchangeable, and compile to the same IL. There should be one key rule in your coding standards - if you are editing a shared file, use the same style as the existing code. Nothing is more annoying than trying to fix a file where the style changes every other function.

So - pick one and be consistent.

Spring初心 2024-07-26 18:55:29

使用任一选项都没有技术原因。 两者在生成的 IL 方面是相同的。

然而,最大的原因是一致性和其他人的可维护性。 使用“string”、“int”、“double”等更符合预期,因此更易于维护。

另外,如果您没有 using System;,则始终输入“int”比“System.Int32”要好得多......

There is no technical reason to use either option. Both are identical in terms of the generated IL.

However, the largest reason is consistency and maintainability by others. Using "string", "int", "double", etc is more expected, and therefore more maintainable.

Also, if you don't have a using System;, it's much nicer to type "int" than "System.Int32" all of the time...

最近可好 2024-07-26 18:55:29

没有任何理由会影响应用程序的行为或性能。 这是推荐的风格指南 - 与所有风格指南一样,如果您认为合适,可以忽略它。 几乎所有 OSS 和公司风格的策略都会规定您需要使用别名 - 所以请记住这一点。

There is no reason that would affect the behavior or performance of your application. It is a recommended style guideline - and as with all style guidelines you can ignore it if you see fit. Nearly all OSS and corporate style policies will stipulate that you need to use the aliases - so keep that in mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文