我应该使用 System.Guid.NewGuid() 还是使用 System 然后使用 Guid.NewGuid()?

发布于 2024-11-30 15:43:59 字数 133 浏览 1 评论 0原文

我什么时候应该使用全名 Sytem.Guid.NewGuid();?对于所有情况,我是否应该始终使用 using System; 然后使用 Guid.NewGuid();

When should I use full name, Sytem.Guid.NewGuid();? Should I always use using System; and then Guid.NewGuid(); for all cases?

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

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

发布评论

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

评论(6

残月升风 2024-12-07 15:43:59

您应该使用后者,即首先包含名称空间。它的优点是只要看到using语句,你就会清楚地知道这个文件中使用了哪些库。

you should use the later, i.e. include namespace first. The advantage of it is by only seeing the using statements, you will be well aware that which libraries are used in this file.

征﹌骨岁月お 2024-12-07 15:43:59

我认为如果您在类/命名空间层次结构的某个级别上有重复的名称,那么使用完全限定名称即 Sytem.Guid.NewGuid() 会更有意义,您希望通过显式告知完整名称来避免这种情况。姓名。

由于 System 是非常独特的命名空间,因此您应该使用 Guid.NewGuid()

I think it will make more sense to use fully qualified name i.e. Sytem.Guid.NewGuid() if you have duplicate names at some level of class/namespace hierarchy which you want to avoid by explicitly telling the full name.

As System is pretty much unique namespace you should go for Guid.NewGuid()

淡淡の花香 2024-12-07 15:43:59

我想说一致性比你选择的替代方案更重要。就我个人而言,我倾向于总是指定 using 指令并让它们按字母顺序排序,因此可以立即看到其中存在或不存在的内容。然后,在我的代码中,我总是使用非限定名称,除非我需要消除具有相同名称的类之间的歧义。

I'd say consistency is more important than which alternative you choose. Personally I tend to always specify using directives and keep them sorted alphabetically, so it's really immediate to see what is or isn't there. Then in my code I always use unqualified names, except when I need to disambiguate between classes with the same name.

我的黑色迷你裙 2024-12-07 15:43:59

我个人不喜欢这么长的标识符。如果代码很多,则很难阅读。
但是,当类型名称之间存在歧义时,完全限定版本可以解决此问题。由于命名空间冲突,我个人只在必要时才使用它们。而且在这种情况下,我更喜欢声明 命名空间别名。这使得代码更具可读性。

无论如何,对于编译后的应用程序来说,没有什么区别,编译后的代码是相同的。

我也遇到过,它们对于某些手动重构操作来说是不切实际的,但也许相反也可能是正确的,我不记得确切的情况......

I personally don't like this long identifiers. The code is very hard to read if you have a lot of them.
However, when there are ambiguities between type names, the fully qualified version resolves this. I personally only use them when I have to, due to namespace conflicts. And also in this case I like more to declare a namespace aliase. This makes the code much more readable.

Anyway, for the compiled app, it makes no difference, the compiled code is the same.

What I also have encountered, that they were unpractical for some mannual refactoring action, but maybe the opposite may also be true, I don't remember the exact case...

甜味拾荒者 2024-12-07 15:43:59

我认为这并没有什么区别。 “使用”在编码时更有用,但在编译为 IL 时,所有类都会编译为其全名。

Doesn't really make a difference, I think. 'Using' is more useful when coding, but when compiling to IL, all classes get compiled to their full name.

冷…雨湿花 2024-12-07 15:43:59

命名空间是 C# 的仅编译时功能,可让您在开发过程中节省时间。编译器使用 using 指令来查找代码中的速记类型名称。

基本上,每次编译器在代码中遇到它不知道的类型名称时,它都会采用每个 using 指令并将其添加到类型名称之前,然后查看该完全限定名称是否可以解析。

一旦你的应用程序被编译,命名空间和 using 指令就会消失,因为 IL 不需要它们。

要回答你的问题,这真的并不重要..如果你经常在单个文件中使用它,那么导入它,否则使用完全限定的命名空间

Namespaces are a compile-time only feature of C# that allow you to save time during development. The using directives are utilized by the compiler to look up shorthand Type names in your code.

Basically each time the compiler encounters a type name in your code that it does not know it takes each using directive and prepends it to the type's name and sees if that fully qualified name resolves.

Once you application is compiled the namespaces and the using directives are gone as the IL does not need them.

To answer your question it really doesnt matter.. if you are using it often in a single file then import it else use the fully qualified namespace

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