在类名中使用下划线

发布于 2024-10-11 05:43:41 字数 93 浏览 2 评论 0原文

在类名中使用 _ 安全吗?

命名中 . 的最佳替代是什么? (目前我使用_。)

Is it safe to use _ in class names?

What's the best replacement for . in naming? (Currently I use _.)

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

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

发布评论

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

评论(6

柠檬 2024-10-18 05:43:42

正如其他人提到的,它是安全的。但是,Microsoft 的类命名指南如下:

请勿使用下划线字符 (_)。

正如 Jon Skeet 提到的,一个好的替代方案是将 . 替换为 Dot。例如,与drive.google.com相关的类可以命名为DriveDotGoogle。 (我认为可以省略 DotCom 部分,以免不必要地延长名称。)

As others have mentioned, it is safe. However, Microsoft's Class Naming Guidelines read:

Do not use the underscore character (_).

As Jon Skeet mentioned, a good alternative would be to replace . with Dot. For example, a class related to drive.google.com could be named DriveDotGoogle. (I think the DotCom portion could be omitted so as not to unnecessarily lengthen the name.)

弥繁 2024-10-18 05:43:42

是的,它很安全。

您甚至不应该考虑有一个“。”在类名中。我无法想象他们什么时候会出现。也许如果您想将某个东西命名为“.NET”,您可以使用“DotNet”。类名应该简洁,就我个人而言,我从未在类名中使用过下划线。我更喜欢骆驼肠衣。

您可以考虑阅读MSDN - 命名指南

Yes, it is safe.

You should never even consider having a "." in a class name. I can't imagine when they would come up. Perhaps if you wanted to name something ".NET" you would do with "DotNet". Class names should be concise, and personally, I've never used an underscore in one. I prefer camel casing.

You may consider reading MSDN - Naming Guidelines.

简单爱 2024-10-18 05:43:42

对于类名,我使用 CamelCasing 而不是 _。您应该坚持一个标准,如果您正在与多个人一起处理项目,这一点尤其重要。命名约定应在解决方案/组织内保持一致

For class Names, I use CamelCasing rather than _. You should stick to one standard, this is specially important if you're working on projects with more than one person. Naming convention should be consistent within the solution / organisation

落花随流水 2024-10-18 05:43:42

在类名中使用 _ 是安全的,就像使用其他标识符一样。

为什么类名中需要逻辑 . ?你能举个例子吗?

It is safe to use _ in class names, just like it is with other identifiers.

Why do you need a logical . in your class names? Can you give an example?

椒妓 2024-10-18 05:43:42

听起来像是您生成的 CSharp 代码。如果是这种情况,您可能需要查看 System.Design 程序集中的以下帮助程序例程:

http://msdn.microsoft.com/en-us/library/ms145952.aspx

static string FixName(string givenName)
{
    return System.Resources.Tools.StronglyTypedResourceBuilder.VerifyResourceName(
        givenName, new Microsoft.CSharp.CSharpCodeProvider());
}

Sounds like your generating CSharp code. If this is the case you may want to look at the following helper routine from the System.Design assembly:

http://msdn.microsoft.com/en-us/library/ms145952.aspx

static string FixName(string givenName)
{
    return System.Resources.Tools.StronglyTypedResourceBuilder.VerifyResourceName(
        givenName, new Microsoft.CSharp.CSharpCodeProvider());
}
人间☆小暴躁 2024-10-18 05:43:41

一次使用一个下划线是安全的,尽管您不应该使用两个连续下划线。来自 C# 语言规范,第 2.4.2 节:

包含两个连续下划线字符 (U+005F) 的标识符保留供实现使用。例如,实现可能提供以两个下划线开头的扩展关键字。

就我个人而言,无论如何,我都会尽量避免使用带下划线的类型名称...不过,我不确定“替换 .”是什么意思。如果您能提供一些背景信息,那将会有所帮助。

It's safe to do so for one underscore at a time, although you shouldn't use two consecutive underscores. From the C# language spec, section 2.4.2:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.

Personally I try to avoid type names with underscores anyway though... I'm not sure what you mean by "replacement for ." though. If you could give some context, that would help.

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