Ever since I started .NET programming after .NET Beta 1 Arrived in 2001, I found that many people struggle with the relation between assemblies and namespaces.
So I was glad that I posted this answer about 2.5 years ago on StackOverflow. Below is the slightly edited form:
People are easily confused by the namespace/assembly thing, as it decouples the concept of where your code is physically located (the assembly) and how you reference it:
- logically reference is by using the namespace
- physical reference is by referencing the assembly
I usually explain the relation using the word contribute:
- An assembly can contribute to multiple namespaces.
For instance, theSystem.Data.dll
assembly contributes to namespaces likeSystem.Data
(e.g. the classSystem.Data.DataTable
) andMicrosoft.SqlServer.Server
(e.g. the classMicrosoft.SqlServer.Server.SqlContext
).- Multiple assemblies can contribute to a single namespace.
For instance both theSystem.Data.dll
assembly and theSystem.Xml.dll
assembly contribute to theSystem.Xml
namespace.
Which means that if you use theSystem.Xml.XmlDataDocument
class from your project, you need to reference theSystem.Data.dll
assembly.
And if you use theSystem.Xml.XmlDocument
class, you need to reference theSystem.Xml.dll
from your project.(the above examples are .NET 4.0, but likely hold for previous .NET versions as well).
Danny Thorpe explained the concept of namespace and internal really well, so I won’t go into detail about those.
Ever since I started .NET courses 10 years ago, I draw a table explaining assemblies and namespaces like this:
Assembly | Namespaces it contributes to | ||
---|---|---|---|
↓ | System.Data | Microsoft.SQLServer.Server | System.Xml |
↑ Example classes | |||
System.Data.dll | DataTable | SqlContext | XmlDataDocument |
System.Xml.dll | — | — | XmlDocument |
–jeroen