6

Disclaimer: I come from a PHP background.

In PHP, I could have thousands files, which are never loaded, if not needed, due to the autoloader feature (If some code is needed, it would be loaded)

How do .NET's assemblies work? Do they load all the program code into the memory on application startup?

For example, I can have some areas with multiple Controllers:

  • Area 1
    • Controller 1
  • Area 2
    • Controller 1
    • ...
    • Controller 42

If Area 2 is optional by configuration, would it be a better Idea to extract this Area into a seperate Assembly and load it if needed?

5

Do they load all the program code into the memory on application startup?

NO

You can have as many Assemblies in your project as you need. Assemblies are referenced via the using keyword. Unused assemblies are filtered out at the build time itself. For example, you can have two referenced asssemblies as:

using System.Windows.Forms;
using System.Web.UI.WebControls;

If you haven't used assembly System.Windows.Forms, it will not affect the performance of your code but it may affect compile time and Intellisense performance.

See this StackOverflow question for unused usings.

Referenced assemblies are not immediately loaded - they are loaded on the fly as needed. So regardless of whether you have an assembly reference in a top level project, or a dependent assembly of assemblies, assemblies are typically load on an as needed basis, unless explicitly loaded by user code. The same is true of dependent assemblies. See When does a .NET Assembly Dependency get loaded for more info.


[EDIT]

The classes in any assembly are loaded only when they are used. Not all the classes will be loaded at once in the memory. Even static classes are loaded in the main memory when they are first referenced. So having bulk of unused classes in your assembly will not degrade the performance but will increase the hard disk space required(not memory requirements).

So the simple answer is NO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.