简体   繁体   English

System.Net和没有System.Net之间的区别

[英]The difference between with System.Net and without System.Net

What is the difference between adding System.Net like this: 添加System.Net什么区别:

 CookieContainer globalcontainer = new System.Net.CookieContainer();

and using the class without the namespace in the declaration 并在声明中使用没有命名空间的类

 CookieContainer globalcontainer = new CookieContainer();

Which is better in efficiency? 哪个效率更高?

Neither is more efficient, one is explicity specifying the namespace, the other is implicitly. 两者都没有更高效,一个是明确指定命名空间,另一个是隐式。

At the top of your .cs file, the using directives import namespaces, meaning that types in those namespaces don't need the fully qualified path to be recognised in the code 在.cs文件的顶部,using指令导入名称空间,这意味着这些名称空间中的类型不需要在代码中识别完全限定的路径

eg 例如

List<T> appears in System.Collections.Generic ... without the using directive for this namespace you must use the fully qualified name: List<T>出现在System.Collections.Generic ......没有此命名空间的using指令,您必须使用完全限定名称:

System.Collections.Generic.List<int> someList;

Whereas with it, you don't 而它,你没有

using System.Collections.Generic;

List<int> someList;

Sometimes there can be namespace collisions - imagine the following scenario: 有时会出现名称空间冲突 - 想象一下以下情况:

Some.Namespace.Task
Some.Othernamespace.Task

If you import both namespaces: 如果导入两个名称空间:

using Some.Namespace;
using Some.Othernamespace;

Task someTask; // <--- this line will cause a compile time error

The compiler doesn't know which Task you want, the one from Some.Namespace or the one from Some.Othernamespace - in this case you need to be specific and supply the full namespace (or use an alias) 编译器不知道你想要哪个Task ,来自Some.Namespace的那个或来自Some.Othernamespace的那个 - 在这种情况下你需要是特定的并提供完整的命名空间(或使用别名)

Hope this helps 希望这可以帮助

Read all about namespaces here: 阅读所有关于名称空间的内容:

http://msdn.microsoft.com/en-gb/library/z2kcy19k(v=vs.80).aspx http://msdn.microsoft.com/en-gb/library/z2kcy19k(v=vs.80).aspx

In run-time the two will often be the same. 在运行时,两者通常是相同的。

For readability, I often prefer not using the namespace in the same statement (put it in using directive on the top of the file) because it usually saves me space, allowing my lines not to go too long, it's admittedly a small benefit. 为了便于阅读,我通常不喜欢在同一个语句中使用命名空间(将它放在文件顶部的using指令中),因为它通常可以节省空间,使我的行不会太长,这无疑是一个小小的好处。

These are times when I do include the namespace: 这些是我包含命名空间的时候:

  • When I know the class name is likely to be misunderstood, sometimes there are types used in different namespaces in .NET, and I'd like the reader of the code to get what I mean faster 当我知道类名可能被误解时,有时会在.NET中的不同命名空间中使用类型,我希望代码的读者能够更快地得到我的意思

  • When it's the only call from this namespace, sometimes I include the namespace explicitly. 当它是来自此命名空间的唯一调用时,有时我会明确地包含命名空间。

  • If I'm importing multiple namespaces that contain the same type. 如果我正在导入包含相同类型的多个名称空间。 this one is obvious though. 这一点很明显。

So, the answer, as good or bad as this may sound, is "it depends". 所以,答案,无论好坏,这听起来都是“取决于”。 Whatever makes the code more readable in less time wins. 无论是什么使得代码在更短的时间内更具可读性。

http://gurustop.net http://gurustop.net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM