1

I have a problem understanding the difference between namespaces and assemblies. So let's say that I make open Visual Studio, and I create a new project. I will name the project "Project A". The Solution Explorer will look like this:

Project A-Start

Now, as far as I understood, the "Solution 'Project_A'(1 project)" is the assembly & "Project_A" that is right under it is the first namespace. Now, I know that I can add multiple "nested" namespaces with different classes. So I can make another class called X and then make a new folder in "Project_A", so a new namespace that will be called "MainClasses" and add the classes A & B there so that it would look like this:

enter image description here

So now, if I'm not wrong: I have the assembly "Project_A" that has the namespace "Project_A". The namespace "Project_A" includes a class called X & another namespace with classes A & B.

Now, if I go to "Solution 'Project_A'(1 project)" and I click on Add->New Project, I will make a new namespace with the name "Project_B", and add another class to the new namespace called Y, I will now have: The assembly "Project_A" that will contain the namespace "Project_A" & "Project_B", and it will look like this:

enter image description here

Can somebody please correct me if I am wrong and tell me the right way. So what is the exact difference between namespaces & assemblies when working with c# in visual studio. Showing some screenshots would be the best, if you can do it, of course. Thank you

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
David
  • 624
  • 1
  • 6
  • 21
  • 1
    1 project generally produces 1 assembly. It's going to be the binary product of the code. Like an .exe or a .dll file. A namespace is completely different. It's just a way to distinguish your types from others and is completely arbitrary. By default, it follow your project/folder structure, but nothing is preventing you from going into your files and changing the namespace to whatever you want. If you change it to say `namespace Foo` then you'd access types defined in that namespace by something like `Foo.MyClass`. – itsme86 Sep 17 '20 at 17:15
  • 1
    You can think of namespaces like domain names. You might have thousands of computers named "computer1" across the world, but you can distinguish one from another because there can only be one computer1.domain1.com. Similarly, you can have a bunch of assemblies with a class called MyClass, but you won't have an issue as long as multiple assemblies associated with your project don't use the same namesapce for `MyClass`. You can even have multiple `MyClass` classes in your project code as long as they're in different namespaces. Used frequently in models like `Data.User` and `Business.User`. – itsme86 Sep 17 '20 at 17:25
  • 1
    Namespace "Project_A" is the same-name namespace that automatically created after the solution is created, you can change it to any name you want. It has nothing to do with assembly. – 大陸北方網友 Sep 18 '20 at 05:45

3 Answers3

3

An assembly is an exe (executable) or a dll (dynamic link library) and it is a software primary "component" (not in the sense of OOP component or control). Sometimes named package.

What exactly is an Assembly in C# or .NET?

https://learn.microsoft.com/dotnet/standard/assembly/

A namespace is a code organization feature.

https://www.tutorialspoint.com/csharp/csharp_namespaces.htm

https://learn.microsoft.com/dotnet/csharp/programming-guide/namespaces/

An assembly that is like a partition can contains one or more namespaces that are like folders.

When an assembly is added to the references of the project, you can access to all its namespaces with the using directive at the beginning of the file or by specifying full access directly in the code.

Example

The assembly System.dll contains several namespaces like System and System.IO as well as System.Threading and so on.

using System.IO;

var lines = File.ReadAllLines(...);

Or:

var lines = System.IO.File.ReadAllLines(...);

enter image description here
      ...

2

These two concepts are not related. It's only by default that your initial namespace takes the name of your project.

By default each of your projects, contain the global namespace and your own. You can rename the default name of your namespace to anything you want without an issue.

The assembly:

Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (.dll) files, and are the building blocks of .NET applications. They provide the common language runtime with the information it needs to be aware of type implementations.

The namespace:

Namespaces have the following properties:

  1. They organize large code projects.

  2. They are delimited by using the . operator.

  3. The using directive obviates the requirement to specify the name of the namespace for every class.

  4. The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
1

The namespace for the project is set in the project properties. It is by default set to the assembly name and class files inherit this name when created, but you can change to any name you like. If you add a folder and put a file in it, the folder name gets appended to the parent (for the first folder this is the assembly) namespace. Again you can change this to any arbitrary name.