在 C# 中声明 const 更优雅的方式是什么

发布于 2024-09-13 18:05:10 字数 321 浏览 4 评论 0原文

可能的重复:
C# 常量命名约定?

我正在用 C# 重构一个库,然后我发现了很多大写常量:

INTERVAL, TIME, SECONDS.

我认为这是不必要的,我个人更喜欢用驼峰式大小写来声明所有内容。关于更好的方式存在一些确切的定义吗?

Possible Duplicate:
C# naming convention for constants?

I'm refactoring a library in C# and I found a lot of upper case constants:

INTERVAL, TIME, SECONDS.

I think this a kind of unnecessary, and personally I prefer declare everything with camel case. Exist some exactly definition about the better way?

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

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

发布评论

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

评论(7

独自唱情﹋歌 2024-09-20 18:05:10

最终,这种情况不会产生任何影响(除非它与类型/关键字/等发生冲突)。所以,一致性确实是最重要的。

大写约定不区分常量和其他成员 - 但这些仅指南。所以这将是帕斯卡大小写

Ultimately the case isn't going to make any difference (unless it collides with a type/keyword/etc). So really consistency is the main thing.

The Capitalization Conventions don't distinguish between constants and other members - but these are guidelines only. So it would be pascal-case.

蓝海 2024-09-20 18:05:10

全大写常量是一个常见的约定……

但约定就是这样,并且是任意的。如果您正在为自己编写代码,则没有令人信服的理由不选择自己的代码。如果您与其他开发人员合作,则必须就命名约定达成一致。

如果您正在编写的内容将被团队之外的其他人使用,那么您最好坚持使用最常见和最容易识别的命名约定,以避免混淆。

最后,一致性才是最重要的。

All-caps constants are a common convention…

…but a convention is just that, and is arbitrary. If you are writing code for yourself, there is no compelling reason not to choose your own. If you work with other developers, you must all agree on a naming convention.

If you are writing something that will be consumed by others outside your team, you might do well to stick to the most common and recognizable naming conventions to avoid confusion.

In the end, consistency is what counts.

離殇 2024-09-20 18:05:10

这完全取决于您的团队/团队在定义编码指南时选择的标准。

如果其他人都使用 ALL_UPPER_CASE,那么你应该排队。

就我个人而言,我更喜欢对常量使用大写,这样我只需查看它们就知道它们是什么。

It's all up to the standards that your group/team chose when they defined their coding guidelines.

If everybody else uses ALL_UPPER_CASE, then you should fall in line.

Personally, I prefer to use upper case for constants just so I know what they are simply by looking at them.

喜你已久 2024-09-20 18:05:10

有关常量的 MSDN 页面 建议将常量视为静态字段成员。在这种情况下,大写约定表明 PascalCasing 是合适的。

如果您的常量是公共 API 的一部分,我建议遵循此约定。

但是,如果常量只是私有成员,则可以使用您希望的任何约定。在这种情况下,MSDN 实际上在常量帮助页面中有 小写常量成员< /a>,例如。

The MSDN page on constants suggests that constants should be treat like static field members. In this case, the Captialization Conventions would suggest that PascalCasing is appropriate.

If your constant is part of a public API, I would recommend following this convention.

If the constant is just a private member, however, you can use any convention you wish. MSDN, in this case, actually has lower case constant members in the Constant help page, for example.

橪书 2024-09-20 18:05:10

Microsoft 的建议没有提及所有 -大写名称。它们没有明确指定常量的大小写约定,但它们有一个用于枚举值 (PascalCase) 和只读静态字段(也是 PascalCase)的大小写约定>)。因此,根据 Microsoft 的指南,PascalCase 可能是您的最佳选择。

Microsoft's recommendations make no mention of all-uppercase names. They do not explicitly specify a casing convention for constants, but they have one for enum values (PascalCase) and read-only static fields (also PascalCase). So according to Microsoft's guidelines, PascalCase is probably your best option.

温柔一刀 2024-09-20 18:05:10

我通常对公共常量使用 PascalCase,对私有常量使用 CamelCase。例外情况是从较旧的 C/C++ 库或类似库(例如与 P/Invoke 一起使用的库)导入常量时。 - 我保留它们,因为它们是在原始库中编写的。

I generally use PascalCase for public constants and camelCase for private ones. The exception is when the constants are imported from an older C/C++ library or similar (such as those use alongside P/Invoke). - I keep them as they were written in the original library.

苍暮颜 2024-09-20 18:05:10

这都是偏好,但我相信大写字母是对 #define 的回归,因为据我所知, const 与它相差不远。

更具体地说,我相信大写 const 只是另一种描述方法,您在方法中看到全部大写的东西,并且您知道它是一个 const,如果它是驼峰式大小写,那么它是本地的,带有 _ 的驼峰式大小写是成员私有的,而帕斯卡大小写是会员公众。

虽然这只是一些人喜欢的一致性标准之一,但它确实是优先的,尽管我认为原因正如我所说,只是为了让你在看到所有大写字母时清楚地知道它是一个常量。

This is all preference, but the upper case is I believe a throw back to #define because a const isn't a far cry from it as I understand.

More specifically, I believe the upper case const is just another delineation method where you see in a method something in all caps and you know it's a const, if it's camel case it's local, camel case with _ is member private, and pascal case is member public.

Though this is just one standard of consistencies which some prefer, it really is preferential, though I think the reason is as I said, just to make it obvious when you see all caps you know it's a const.

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