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

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.

Related

Xamarin Studio how to write namespaces

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"

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.

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.

Changing namespace using vstemplate?

I'm creating a custom project template in VS 2010, and I want to add prefix to all of these projects (it is a template for sub-projects within a solution).
So, if I create "NewProject" I want it to have the namespace "MasterSolution.NewProject".
I can manually alter the namespace within the .cs files, so if I had a Class1.cs then it says
using MasterSolution.NewProject
but intellisense only picks up "NewProject", not "MasterSolution".
Is there an entry in the .vstemplate that can alter the namespace?
Thanks

Class modifier for automatically generated classes

When I add a new class to a project, the class modifier/access specifier for the newly created class in Visual Studio is ommitted, thereby making it internal.
Is there a way I can specify in Visual Studio settings that whenever I ask for a new class to be created, please make the class public?
I looked in the Tools->Options menu but couldn't find anything. I am guessing it could be in the Templates folder in the My Documents\Visual Studio xxxx folder.
Yes, you can change the file templates (or simply provide secondary file templates), like so. However, before you do that, I would ask myself: does it really take that much effort to add the desired accessibility? I tend to err on the side of the pragmatic when it comes to this... if I wanted significantly different templates, I might do it. Otherwise... meh.
You would have to create your own template to do this, or use code snippets, or perhaps a tool like ReSharper, which has its own snippet mechanism.
However, this is likely to be a bad idea. You will be making things public that do not need to be public. You're better off starting everything as internal, and making them public only as required.
A quick search turned up this:
Prevent Visual Studio from adding default references and usings for new classes
The answer demonstrates that you can change the default class template.
Hope that helps.
If you omit the modifier on a class it is either:
internal (non-nested classes)
private (on nested classes)
I just thought I'd throw that out there (that it's not ALWAYS internal if omitted).
But, if you really want to change templates you'll want to generally go to the installation folder (I'll use the default folders):
**C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE**
In there, you will find the various template folders. You're looking for the following (if you dig deeper): C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Just alter the Class.cs file in there. Oh, and in case you mess it up here's the original version:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}
You're desired version:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
public class $safeitemrootname$
{
}
}

Categories