0

My question refers to a referenced class library in .NET. Suppose I have a class library with 4 classes, solving various tasks.

In another project I reference the above-mentioned class library and use explicitly only 1 class from the whole class library.

Does the executable project use the entire class library or does the compiler extract out only the used class to avoid the 'unnecessary' overhead(unused other classes from the class library) ?

I have a big utility class library, should I divide it into more smaller class libraries or are there any optimizations when using class libraries to decrease the memory space etc.?

GrayFox
  • 997
  • 1
  • 9
  • 26
  • 3
    What do you expect as _unnecessary overhead_? Related: http://stackoverflow.com/questions/801252/can-i-separate-a-big-dll-into-1-dll-per-class – Tim Schmelter Jan 29 '16 at 10:41
  • How big is "big" here? Have you actually noticed any issues due to this? IMO, a dependency would have to be really pretty huge before it became an issue in most scenarios. (Mobile is *possibly* an exception to this, but I suspect that in most mobile applications, the image assets etc end up dwarfing dependencies...) – Jon Skeet Jan 29 '16 at 10:44

4 Answers4

2

.Net does not statically link libraries so the project which is referencing your class library doesn't "extract" anything out of the class library.

kjbartel
  • 10,381
  • 7
  • 45
  • 66
2

Does the executable project use the entire class library or does the compiler extract out only the used class to avoid the 'unnecessary' overhead?

Class libraries compile to DLLs (Dynamically Linked Libraries). To answer your question, no, the compiler does not "extract" out the used classes from the library. The CLR will load the class from the library when it is needed, it won't load anything into memory that isn't required during runtime.

I have a big utility class library, should I divide it into more smaller class libraries or are there any optimizations when using class libraries to decrease the memory space etc.?

Opinion based, but you could split down your utilities by activity and then reference by dependency, that way you wont need to release the whole library when you build an application.

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
1

To answer your question: The library gets used in its entirety.

In C/C++ a statically linked library would work differently and only import the code that was necessary, but even with C/C++ a dynamically linked linked library would use the entire library.

C# is more closely aligned to the dynamically linked library and as such will use the entire library.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Jens Meinecke
  • 2,904
  • 17
  • 20
0

Usually, all classes which are included in the project will be compiled into the assembly. However you can exclude some classes from compilation in your IDE, which would reduce the size of the assembly.

Whether you should split your library in smaller class libraries depends mainly on whether these parts could be used independently. But I would not try to split it just to optimize for size. I would definitely not try to split a library consisting of only four classes in multiple libraries.

proskor
  • 1,382
  • 9
  • 21