Jude Cooray
Jude Cooray

Reputation: 19862

vs using [namespace.x.xx]?

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 :)

Upvotes: 1

Views: 194

Answers (2)

drharris
drharris

Reputation: 11214

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.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500485

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.

Upvotes: 6

Related Questions