Xamarin Studio how to write namespaces - c#

In C# is normal to write namespace
namespace ProjectName.Folder so if project is Test and then folder name inside project is Utils then namespace is
namespace Test.Utils
In Xamarin projects in Github namespace is always the same so just
namespace Test
so what is now better to use, always the same namespace or like in Visual studio?

This is mostly personal preference. Xamarin Studio by default uses a single namespace per project, which is probably why most examples you see are written that way. There is an option you can change to make it behave more like Visual Studio
Preferences --> Source Code --> .NET Naming Policies --> "Associate
namespaces with directory names"

Related

How to use a "flat" namespace in whole C# project

I want to use the same namespace for my whole c# class library. But the default behavior for new files is to use a namespace like [Base namespace].[Folder 1].[Folder 2].
Is there a simple way or plugin for visual studio 2019 to make all new files use the same namespace? So just [Base namespace] whatever the folder?
But just for a single project, I don't want to change the global file templates.
Although all concerns mentioned in comments, there's simple way to achieve this. Just go to project properties and define default namespace:
Now every added class has this namespace defined:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyGlobalNamespae
{
class Class1
{
}
}
I have found a workaround I'm pleased with:
I installed this nice extension Visual Studio Namespace Fixer. It allows you to define a template for your namespaces and then can apply it to all .cs, .xaml, and other files in your project.
Under Options -> Namespace Fixer options -> Use default project namespace I defined the format {projectRootNamespace} for Namespace format.
Not I just have to select my files and folders in the projects base folder (in Visual Studio, not in the windows explorer), rightclick and select Adjust namespaces and all files in all subfolders will change their namespace to the projects root namespace :-)
Awesome if you create and use a lot of class and other files in your NuGet lib.

Visual Studio C# Namespaces And Assemblies

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.

Adding the solution name as base of namespace for all projects

So I have this solution in visual studio 2013. Currently the solution tree looks like this:
MySolution
>MyProjectA
>MyProjectB
>MyProjectC
The default is that each class in the project folder has the namespace same as the project folder name.
But I would like that each class in each project folder have a namespace that starts with the solution name. For example if there is a class named MyClass in MyProjectA I want it's namespace to be MySolution.MyProjectA
Is there a way to automatically to this in Visual Studio? I could go and change all the namespaces my self...but I rather would love to see if its possible to do this automatiaclly.
In each project's settings, there is a Default Namespace that is used whenever you add new files to a project. The default value is the project name, but you can customize it. You can change that and then any new files added will use the desired namespace.
For any existing files, a Find and Replace can provide a relatively quick cleanup (find: namespace MyProjectA -> replace with: namespace MySolution.MyProjectA, only search within current project).
Download ReSharper, it has a lot of such features. Install it, after that you just have to rename each project as MySolution.ProjectA,B,C and then right click on the project and select option refactor and then click adjust namespaces. This will do the trick, only thing I see here is that you will need to rename your projects.

Visual Studio namespace convention

I want that all of the files in an assembly will have the assembly name as namespace.
For example, if I create a new class in MyAssembly.SomeFolder - I want that the class defualt namespace will be "MyAssembly" and not "MyAssembly.SomeFolder.
Is there a way to configure this on VS?
You can change the Visual Studio class template for C# to bypass this.
You may want to look into the Visual Studio templates here: http://msdn.microsoft.com/en-us/library/ms247121.aspx
There are no real settings for this, unless you have ReSharper installed. Then you can set Namespace Provider to false.

I can't see a referenced class of another namespace in C#

I have 2 projects:
ConstrainedScheduleInterfaces
ConstrainedSchedule that has a folder (Testing) with my class that need the reference, here's the code:
Tests.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using
using NUnit.Framework;
using Ninject;
using ConstrainedScheduleInterfaces;
namespace ConstrainedSchedule.Testing
{
internal static class Configuration
{
...........
}
}
I added the reference to the ConstrainedSchedule project, but the using ConstrainedScheduleInterfaces; is marked red as not found.
Both the project has destination framework set .NET Framework 4.5
Any help? Thanks
Does the project contain a reference to the other project? You can't just add the namespace, the project itself needs an assembly reference to the other project which has that namespace.
In Visual Studio, open the Solution Explorer. Right-click on the ConstrainedSchedule project and select something along the lines of "Add Reference." In the dialog, select project references (depending on the version of Visual Studio it may be a tab or some other interface indicating projects as part of the solution). Select the ConstrainedScheduleInterfaces project and add the reference.
More information here.
For other people who have this problem, who have already added the reference to the dll and have made sure you have the right namespace at the top, I've found another thing that can cause this.
Select-click the file in visual studio and choose properties. In the Advanced part of properties, look for Build Action. Change it to compile if it's not already on that, and try to build again.
There might be another reason: different target frameworks.
My main project had ".NET Framework 4.7.2" and the referenced project (linked from another solution) had ".NET Framework 4.5.1".
Select the project properties and change the target framework.
I was having this issue after renaming a project and namespace due to a naming conflict.
Basically my main project had a reference to a project I made called Common, but then I needed to reference another project called Common that another dev had written. After renaming my project something like SpecificCommon, I was able to have references to both Common and SpecificCommon in my main project. However, all references to SpecificCommon namespace were now broken.
At first I thought this was some problem with the namespace refactor, but it turns out that it was because SpecificCommon and Common both still had the same assembly name (Common.dll). Changing the assembly name in SpecificCommon's properties and rebuilding solved the issue for me.

Categories