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

Wen*_*Lam 2 c# system.net

添加System.Net这样的有什么区别:

 CookieContainer globalcontainer = new System.Net.CookieContainer();
Run Code Online (Sandbox Code Playgroud)

并在声明中使用没有命名空间的类

 CookieContainer globalcontainer = new CookieContainer();
Run Code Online (Sandbox Code Playgroud)

哪个效率更高?

Cha*_*leh 7

两者都没有更高效,一个是明确指定命名空间,另一个是隐式.

在.cs文件的顶部,using指令导入名称空间,这意味着这些名称空间中的类型不需要在代码中识别完全限定的路径

例如

List<T>出现在System.Collections.Generic...没有用指令为这个命名空间必须使用完全合格的名称:

System.Collections.Generic.List<int> someList;
Run Code Online (Sandbox Code Playgroud)

而它,你没有

using System.Collections.Generic;

List<int> someList;
Run Code Online (Sandbox Code Playgroud)

有时会出现命名空间冲突 - 想象一下以下场景:

Some.Namespace.Task
Some.Othernamespace.Task
Run Code Online (Sandbox Code Playgroud)

如果导入两个名称空间:

using Some.Namespace;
using Some.Othernamespace;

Task someTask; // <--- this line will cause a compile time error
Run Code Online (Sandbox Code Playgroud)

编译器不知道Task你想要哪个,Some.Namespace中的那个或Some.Othernamespace中的那个 - 在这种情况下你需要是特定的并提供完整的命名空间(或使用别名)

希望这可以帮助

阅读所有关于名称空间的内容:

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