Namespace or assembly?

I am very confused between namespaces and assemblies. Are System.Data and System.Web namespaces or assemblies?

I noticed that they are called namespaces and at the same time they are present in the GAC_32 folder. So what exactly are they?

+60
c # namespaces assemblies
Feb 03 '14 at 2:54
source share
8 answers

System.Data is the namespace, System.Data.DLL (file) is an assembly.

A namespace is a logical grouping of types (mainly to avoid name clashes). An assembly can contain types in several namespaces ( System.DLL contains several ...), and one namespace can be distributed across assemblies (for example, System.Threading ).

+87
Feb 03 '14 at
source share

A namespace is a logical grouping of classes that belong to the same functionality. So System.Web and System.Data are namespaces

MSDN describes this as:

Namespaces are heavily used in C # programming in two ways. First, the .NET Framework uses namespaces to organize its many classes. Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects.

namespace

Assembly is a piece of (precompiled) code that can be run by the .NET runtime. It contains one or more namespaces. A .NET program consists of one or more assemblies.

System.Web.dll and System.Data.dll are assemblies.

MSDN describes this as:

Assemblies are the building blocks of the .NET Framework applications; they form the core deployment unit, version control, reuse, scope, and security permissions. An assembly is a set of types and resources that are created to work together and form a logical unit of functionality. The assembly provides a common language runtime with information that should be aware of type implementations. For the runtime, the type does not exist outside the context of the assembly.

assembly

+43
Feb 03 '14 at 17:01
source share

In short:

Assembly:

An assembly is a fundamental unit of the physical grouping of code. This is an output module. It is a deployment unit and version control unit. Assemblies contain MSIL code.

Namespace:

The namespace provides the basic unit of grouping of logical codes. This is a collection of names in which each name is unique. They form a logical boundary for a group of classes. The namespace must be specified in the project properties.

+13
Feb 03 '14 at
source share

They are namespaces. Collections contain more than one namespace. For example: System.dll contains these namespaces (and more):

enter image description here

Also, a single namespace can contain nested namespaces. These are just logical names for organizing the code. Keep in mind that DLL are assemblies that contain namespaces.

GAC Global assembly cache . According to MSDN:

The global assembly cache stores assemblies specifically designed to be shared between several applications on a computer.

Frequently used assemblies are stored in the GAC , and therefore you do not need to copy all the assembly files to your project directory that references your project. Assemblies stored in GAC , Strong-Named . Usually, when you add an assembly link from your project that is not Strong-Named , a copy of your .dll file will be created in your bin\Debug folder .. If you want to make your assembly (for example, a class library project) Strong-Named .See: How to register an assembly with a strong name

+12
Feb 03 '14 at
source share

The file that you see in the GAC, System.Data.dll , is an assembly and contains namespaces, including System.Data . If you look at the Reference properties in Visual Studio, you will see:

enter image description here

Later, if you right-click on the link and select a view in the Object Explorer, you will see the namespaces in this particular assembly.

enter image description here

+8
Feb 03 '14 at 15:03
source share

In short:

  • The assembly is stored as .EXE or .DLL files.
  • A namespace is a way of grouping type names and reducing the likelihood of name collisions.

Advice.

An assembly contains a set of types (for example, the l'assembly System contains many namespaces included by System, System.IO, ecc). As a rule, the assembly name matches the name of the space that it contains, but not always.

Another example of assemblies and namespaces.

Assembly 1 ( CoreAssembly.DLL )

Contains Namespace Namespace1.subnamespace1

Assembly 2 ( ExtensionCoreAssembly.DLL )

Contains Namespace Namespace1.subnamespace1

You can use the name of an assembly that contains different namespaces and extend the existing assembly with another assembly using this technique.

DEFINITIONS

Assembly

An assembly is a collection of types and resources that form a logical unit of functionality. All types in the .NET Framework must exist in assemblies; The common runtime does not support types outside of assemblies. Each time you create a Microsoft Windows® application, a Windows service, a class library, or another application with Visual Basic.NET, you create a single assembly. Each assembly is stored as a .exe or .dll file. Note. Although it is technically possible to create assemblies that span multiple files, you are unlikely to use this technology in most situations.

Namespaces

Another way to organize your Visual Basic.NET code is to use namespaces. Namespaces do not replace assemblies, but a second organizational method that complements assemblies. Namespaces are a way of grouping type names and reducing the likelihood of name collisions. A namespace can contain both other namespaces and types. A fully qualified type name includes a combination of namespaces that contain this type.

Link: http://msdn.microsoft.com/en-us/library/ms973231.aspx

+8
Feb 03 '14 at 15:24
source share

Others gave very good and detailed answers to this question. But I want to note that when you are not sure, you can look at MSDN. The MSDN library explains very clearly and simply the namespace and assembly in which this type resides. It even says the file name (in System.Data.dll) , so there is no ambiguity.

enter image description here

+6
Feb 04 '14 at 13:26
source share

As @amdluigi writes: "As a rule, the assembly name matches the name that it contains, but not always."

In Object Explorer in Studio, there is a screenshot from System.Data.dll. This is a great example to study problems here. Note that most of the namespaces contained in the assembly are System.Data or the System.Data namespace.

In general, it’s good that assembly names should be associated with namespaces inside them. Note that when Studio first creates a project to build the assembly, one of the project properties is the default namespace. First, Studio provides a default namespace with the same name as the project itself. If you ever want to rename a project, consider changing your default namespace as well.

There are two additional namespaces: Microsoft.SqlServer is understandable. Some types of SQL Server that they did not want to pack into a separate assembly.

But what about System.Xml ???? There is an assembly of System.Xml.dll. Why is this namespace also displayed in System.Data.dll?

Note that the assembly can re-open the namespace and add more to it - exactly what System.Data.dll does with the System.Xml namespace.

The reason is that namespaces have zero performance values, but there are a lot of assemblies. If you have 1000 classes with a lot of code, you do not want one assembly to have a very large amount of memory. Also you do not want 1000 assemblies each with one class. Any assembly must be loaded into memory before its contents are completed. You want the assembly to contain a reasonable number of interconnected classes, so as soon as your application has loaded the assembly to get one of its classes, it will receive the other classes that the application will most likely need. Granularity is important: not too big, not too small, simple.

Note that System.Data.dll reopens System.Xml and adds exactly one class: XmlDataDocument. It happens that this class is used to interpret relational data as an XML document. If your application just uses XML, it does not need this class. If your application deals with relational data, it can. Thus, although XmlDataDocument is inherited from XmlDocument and located in the System.Xml namespace, it is packaged in the System.Data.dll assembly.

All this is especially important if you have a background with Java, where there is only one concept, a package. There are two in .NET, assembly and namespace. These two are orthogonal. Obviously, an assembly contains more than one namespace. An assembly can reopen the namespace and add more to it — in other words, types in the namespace can span more than one assembly.

+5
Feb 04 '14 at 6:48
source share



All Articles