Visual Studio C# Namespaces And Assemblies - c#

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:
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:
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:
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

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(...);
      ...

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:
They organize large code projects.
They are delimited by using the . operator.
The using directive obviates the requirement to specify the name of the namespace for every class.
The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.

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.

Related

Which assembly will each namespace go into?

Out of curiosity, in my c# project/solution which assembly will each namespace go into? How does it decide which namespace to put into which assembly?
Edit:
Was just a bit confused, I thought that there was some kind of correlation between which namespace go in which assembly.
Each project usually builds one assembly, regardless of namespaces. So there isn't a direct relationship between namespaces and assemblies, since you can declare multiple namespaces in a single project, and can declare the same namespace across different assemblies.
However, the usual recommendation (h.t. CodeCaster) is to name the projects, their output assemblies, and their namespaces consistently, so that it's clear which namespace comes from which assembly. So if you make a project named MyCompany.MyApp.csproj, it by default creates an assembly named MyCompany.MyApp.dll (or .exe), and new files you create will be in the MyCompany.MyApp namespace. You can have child namespaces within your project, typically corresponding to the folder structure in the project, e.g. folder Section contains types in the namespace MyCompany.MyApp.Section. But again, this is all convention - you can change the namespace directives in each file to do something different.
which assembly will each namespace go into?
There is ZERO correlation between namespace and assembly.
The compiled class goes into the assembly created by the project - every project one assembly. Namespaces can be used on many assemblies and there is simply ZERO correlation technically, though CONVENTION says you keep correlated namespaces in one assembly - the compiler does not care.
At the end, TYPES go into an assembly, and TYPES have a namespace - but nothing in the build process cares as decision where it goes.
Keeping it very simple. Assembly is phsical grouping but NameSpace is logical grouping. Assembly can have more namespaces.
System.Data is a namespace, System.Data.DLL is an assembly

ReSharper: override default namespace to prevent "Namespace does not correspond to file location"

Given two projects:
Model, assembly "Company.Product.Model", default namespace "Company.Product.Model"
Model.Server, assembly "Company.Product.Model.Server", default namespace "Company.Product.Model.Server"
As you can see both projects have different default namespaces. It's for sake of resources - both projects has its own resource.resx which will have full names based on project default namespaces.
But both projects have classes related to "model" so I want they to be in the same namespace - "Company.Product.Model". So I use this namespace in both projects.
The problem is that ReSharper requires me to use namespace "Company.Product.Model.Server" for classes in "Model.Server" project as it's default (base) namespace: "Namespace does not correspond to file location...".
I understand it's good by default. But I want to override this - and tell it that all my classes namespaces are based on "Company.Product.Model" without changing project's default namespace.
Is it possible?
If you place the selection on one of the folders in the Solution Explorer, then display the Properties tool window, there is an option there called "Namespace Provider". If you set this to false, ReSharper will no longer suggest your namespaces should include that folder.
See more info in the docs.

"Each namespace should exist within its own assembly" Please explain

I have been given some criteria for a project. It MUST be constructed in a particular way. I know this isn't a great question but my C# is really rusty. The specification states:
Application namespaces should follow (but not be limited to) the below
[actual names changed]:
AnExample.Sample.Foo
AnExample.Sample.Foo.UnitTests
AnExample.Sample.Bar
AnExample.Sample.Bar.UnitTests
and that "each namespace should exist within its own assembly".
I'm a little confused- what is being asked for and how to achieve it? For each namespace to exist in it's "own assembly" does it have to be a separate project and referenced or is it a folder structure?
each namespace would have to be in its own project (.csproj)
within your projects, you could have whatever folder structure you wanted, as along as the namespace in the entire project is the same.
Basically, what they're asking you for is that each namespace is in its own separate file.
A project compiles to an assembly, which can be a .dll or an .exe. What they're saying is that each separate namespace should be in a separate project, so it compiles to a separate file. This is good for decoupling. You can then bring them all together using the Add Reference... button in VS.

Is it safe to change application properties?

I see that my application has the wrong name.
When I go to:
Project -> Application properties...
I see an Assembly name and a default namespace.
Is it safe to just change these two to the values which would better represent my app or will it break something?
Its probably fine to change those two settings:
Assembly name is the name of the output assembly (without the extension)
Default namespace is the name of the namespace that Visual Studio uses when adding new code files.
As long as you don't have some code that depends on the assembly having a certain name you should be fine.
Yes, it is safe. Assembly name is your .exe or .dll file name. Default Namespace, in C#, is the default namespace inserte in top of your files when you create a new file. In VB.NET the "default namespace" means something different (it is prepended to every namespace you define in your project)
Yes, the default namespace is always safe to change. This will just affect what namespace Visual Studio will use when you create a new file in your project. It will not affect existing types; you will have to change those manually.
Assembly name is likewise safe to change.
However, if you have built other software against this assembly or distributed the assembly to others then changing the namespaces of types or the assembly name is a breaking change, and will cause those applications or assemblies to fail if used with a new version of your assembly.
You can certainly change them, but changing them will have consequences. Changing the assembly name simply changes the name of the output DLL or EXE file. The contents are basically unchanged, but anything that references your assembly will need to update the reference.
Changing the root namespace is a little more annoying, but it is also allowed. You will likely end up having to go through a lot of files and fix namespace references, and like renaming the assembly, anything that references your assembly will need to be updated.
From an API perspective, both of these are 'breaking' changes due to the required changes to anyone who references your assembly.
If it is a ClickOnce Application, you will have issues in autoupdate for existing installations.

C# in VS2005: Assemblies listed in the "References" folder in a project

If you have
using XXXX.YYYY;
at the top of a C# file, do you need to include that assembly in the References part of the project?
What is the difference?
The references are needed to be added, so that they may be physically located by the compiler at compile time.
For more details watch it at http://en.csharp-online.net/CSharp_FAQ:_Why_add_a_using_statement_and_a_reference
Hope this helps.
Thanks,
Madhup
The "using" keyword is a way of avoiding having to type out the whole namespace for a class every time if it lives outside the current namespace.
For example, if I have namespace foo and I want to reference MyClass in namespace bar I can either write:
bar.MyClass = new bar.MyClass();
or
using bar;
...
MyClass = new MyClass();
The references part of the project tells the compiler which libraries outside the current project to search for the class bar.MyClass
So in short you don't need to put the using statement (but it generally makes the code easier to read and less for you to type) but you do need the referenced assembly.
You don't write using XXXX.dll at the top of a CS File.
I believe you're referring to using NamespaceX; which is a way of categorizing your classes into distinct logical partitions. So I'd group all of my Data Access classes into a namespace called MyProject.DataAccess. An assembly can contain classes belonging to multiple namespaces.
In which case, you need to reference the assembly X if you want to use some types/classes defined in assembly X with that namespace.
The using statement states you want to import a namespace into the file, giving you shorthand access. For example you can write File.Delete(file) instead of System.Io.File.Delete(file) if you imported the System.Io namespace. The namespace you are including should be available in one of your references assemblies. As fasr as I know, you can't reference DLL's directly like that from your code.

Categories