C# assemblies, whats in an assembly?

后端 未结 5 1208
难免孤独
难免孤独 2020-12-28 14:00

I\'m trying to understand the internal access modifier in C#. I can\'t seem to understand what an assembly is exactly, and what part of my program is held inside that assemb

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 14:30

    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 and physical reference is by referencing the assembly).

    I usually explain this using the word contribute:

    1. An assembly can contribute to multiple namespaces.
      For instance, the System.Data.dll assembly contributes to namespaces like System.Data (e.g. the class System.Data.DataTable) and Microsoft.SqlServer.Server (e.g. the class Microsoft.SqlServer.Server.SqlContext).

    2. Multiple assemblies can contribute to a single namespace.
      For instance both the System.Data.dll assembly and the System.Xml.dll assembly contribute to the System.Xml namespace.
      Which means that if you use the System.Xml.XmlDataDocument class from your project, you need to reference the System.Data.dll assembly.
      And if you use the System.Xml.XmlDocument class, you need to reference the System.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.

    --jeroen

提交回复
热议问题