Reputation: 21
What is the difference between adding System.Net
like this:
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?
Upvotes: 2
Views: 193
Reputation: 36594
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.
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
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.
Upvotes: 3
Reputation: 14002
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
e.g.
List<T>
appears in System.Collections.Generic
... without the using directive for this namespace you must use the fully qualified name:
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)
Hope this helps
Read all about namespaces here:
http://msdn.microsoft.com/en-gb/library/z2kcy19k(v=vs.80).aspx
Upvotes: 7