与使用 [namespace.x.xx] 相比?
我发现您可以执行
using System.IO
并使用
Path.GetDirectoryName(blah blah)
或直接使用
System.IO.Path.GetDirectoryName(blah blah);
从性能的角度来看它们之间有什么区别吗?
using System.IO
是否将命名空间中的所有静态类加载到内存中,从而导致使用更多内存,或者框架是否足够智能,可以避免这种情况?如果是,怎么办?
或者这一切只是为了避免跨命名空间的对象之间的命名冲突?
任何见解将不胜感激:)
I have seen that you can either do
using System.IO
and use
Path.GetDirectoryName(blah blah)
OR directly use
System.IO.Path.GetDirectoryName(blah blah);
Is there any difference between them from a point of performance?
Does using System.IO
loads all static classes in the namespace to the memory resulting in using more memory or is the framework is intelligent enough to avoid that? If yes, how?
Or is this all just used to avoid naming collisions between objects across namespaces?
Any insight would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,它们编译为相同的 IL。这纯粹是源代码的问题 - 通常使用短名称比使用完全限定名称更具可读性。
无论哪种方式,编译结果都是相同的。
No, they compile to the same IL. It's purely a matter of the source code - it's generally more readable to use the short name rather than the fully qualified one.
The compilation results will be identical either way.
如果你看一下IL,这两种方法没有什么区别。所有类名都是完全限定的。静态类仅在首次使用该类时加载。因此,这两种方法在最终代码中是等效的。
此外,我发现浏览
using
声明以查看类在类外部的事物方面正在做什么(例如,类是否执行 I/O 或生成 XML)更有帮助。 )。这与总是声明完全限定的类名相反。If you look at the IL, there is no difference in the two methods. All class names are fully qualified. Static classes are only loaded when the class is first used. So, both methods are equivalent in the final code.
In addition, I find it much more helpful to browse the
using
declarations to see what the class is doing in terms of things external to the class (for example, is the class performing I/O or generating XML). This, as opposed to always declaring fully qualified class names.