-1

Is for example this

using SportsStore.Domain.Entities;

better than this

using SportsStore;

?

If that is the case, what does it mean for the performance of my program when I import bigger libraries?

Edit: I mean, I get that it is pointless to import stuff I don´t use, but is there any actual harm in doing so?

hellogoodnight
  • 1,989
  • 7
  • 29
  • 57
  • 1
    It does completely nothing for the performance. This is importing a namespace, but you will have to reference all library no matter what part of its namespace you are importing. – Konrad Kokosa Apr 25 '15 at 09:44

3 Answers3

2

They're entirely different. A using directive of that form only tells the compiler to make members of the given namespace available as simple names. It has no effect at execution time - if you didn't have any using directives and just referred to everything by the full name, you'd end up with the same code. As "nested" namespaces aren't imported, just using SportsStore won't import members of the SportsStore.Domain.Entities namespace.

(Using directives also make extension methods declared in types within the given namespace available, btw.)

As a rule of thumb, have using directives for everything you need to (in order to keep the bulk of your code simple) but no more than that, just to avoid cluttering your code unnecessarily. Visual Studio has a command to sort using directives alphabetically and remove unnecessary ones - I have that bound to Ctrl+Shift+U which I use fairly frequently.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

There is no performance difference.

"A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification"

So the using statement has no impact of when and how the CLR loads an assembly. It is just syntax so that you can use the types in a cs file without a full qualification.

The CLR lazy loads an assembly once a type of the assembly gets used the first time (ex. If you create an instance of a class, or if you use a static method etc).

So as soon as you create a new instance of a type in your "SportsStore.Domain.Entities", the assembly gets loaded into memory. There is no partial loading of assemblies.

David Roth
  • 677
  • 4
  • 14
0

It's not better or worse. It's semantically different - giving unqualified (direct) access to different sets of entities.

There's no performance effect whatsoever.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90