While I was teaching the SOLID module, as part of the C# Advanced course at SoftUni, I got asked about the difference between a “module” and a “class”. The reason why this question is difficult to answer is because it means different things in different languages and frameworks.
In the purist C# sense, C# itself doesn’t really use the concept of modules in its syntax. You can’t declare, instantiate or import a module in C#. However, we use C# to write code that compiles to .NET assemblies – they contain metadata and bytes that represent Intermediate Language (IL) opcodes, which leads us to modules in .NET.
Modules in .NET
In the sense of .NET, assemblies contain modules, modules contain classes, classes contain functions (and other stuff). You can access assemblies, modules, classes, functions, properties, fields (and the other stuff) through reflection at runtime. This is all interesting, however the C# editing and authoring tools within Visual Studio do not allow you to create assemblies with multiple modules by default.
Although I haven’t tried this, apparently you can create assemblies with multiple modules with notepad.exe and csc.exe, the compiler tools or by using the /addModules command-line option. You can read more about this here: http://etutorials.org/Programming/Programming+C.Sharp/Part+III+The+CLR+and+the+.NET+Framework/Chapter+17.+Assemblies+and+Versioning/17.6+Multi-Module+Assemblies/
When discussing assemblies and modules in the scope of CIL, a module is a single file. In .NET we can have assemblies as a single file, but we can also have assemblies with multiple files. These are called MultiModule assemblies. I found this resource to be pretty good: https://blogs.msdn.microsoft.com/junfeng/2004/07/15/multimodule-assemblies/
To be extra pedantic in the search of the truth, the most trustworthy answer should lie within the Common Intermediate Language (CIL) specification. The official standard is issued by Ecma International, an international organisation that standardises stuff like programming languages, has documented the official CIL standard ECMA-355. Please find the link here: https://www.ecma-international.org/publications/standards/Ecma-335.htm
According to ECMA-355, on page 140 and after, the relationship between assemblies, manifests and modules are defined. Here are some extracts:
- Assemblies – “An assembly is a set of one or more files deployed as a unit. An assembly always contains a manifest…”
- Modules – “A module is a single file containing executable content…”
This is good, we can conclude that an assembly generally has one file / one module, but can have multiple modules (files).
One nice addition, you can use reflection to check out the modules in an assembly. You can research the System.Reflection.Module class: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.module?view=netframework-4.7.2
NOTE: careful… “module class” is a very scary term, but that’s what it is – a class to describe a module. *brain explosion*.
You can also use a tool that reflects code to check out modules in a compiled assembly.
Here is a simple class reflected with my favorite dnSpy:
And here it is with ILSpy:
For further reading, here is a screenshot of page 140 of ECMA-355 (20181120):
OK, so where would this be useful?
- Programming extremely large software can be easier to update with multi-module assemblies.
- Incremental download can be achieved with multi-module assemblies.
In the field of what I do (create business software), we don’t ever need to use multi-module assemblies and very rarely need to worry about modules within assemblies. You can make money without knowing this level of detail.
Other languages
In VB.NET, modules are something different. VB.NET modules are what static classes are in C#. We use VB.NET modules to depict helper functions and extension methods that we don’t want to instantiate, and don’t want to inherit.
What about other languages? I’m a C# guy, so I’m not sure the below is correct:
- Python – in Python, modules allow you to logically organise your code. It is a single file that can contain functions, classes, variables.
- Java – A Java module is a new feature in Java 9. It lets you group Java packages and your application into a module.
- JavaScript – ES6 adopted the module concept. Each module is a piece of code that is executed once it is loaded. Modules can contain functions and variables. Modules can import other modules. Modules are singletons 🙂 It is interesting to check out how the ES5 history makes use of modules, in cases such as CommonJS modules and AMD. I will not go into these here.
Summary
In summary, modules are logical groupings of code. Programming languages implement them differently, leading to changes in the meaning of the term. In some languages, modules are packages. Some languages have other constructs for logically grouping code, such as the namespace concept, which also exists in C#. Most of this “theory” is based on the design of “modular programming”.
Here is a good read is the Modular programming article on Wikipedia: https://en.wikipedia.org/wiki/Modular_programming
Good luck with it all.