Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default namespace in a .NET core 2.1 project

Tags:

asp.net-core

I finally have the opportunity to play around with .NET Core in Visual Studio Code on MacOSX, but obviously a lot of things are different compared to the oldschool .NET Framework/Visual Studio way.

For starters I have not had any luck finding a way to set a default namespace in my .NET Core 2.1 project.

I have tried changing the project name in the .sln file to RootNS.ProjectName instead of the default ProjectName, but although the project name in Solution Explorer is reflecting my change, it does not set the expected namespace when creating new classes - it seems to only follow the folder name.

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RootNS.ProjectName", "ProjectName\ProjectName.csproj", "{443D0B42-6E6C-4DC6-8E42-E90FFCEBD448}"

Am I too naive to even think this is possible?

Thanks!

Edit:

I have already tried adding the RootNamespace element to the .csproj file like so:

<PropertyGroup>
  <TargetFramework>netcoreapp2.1</TargetFramework>
  <RootNamespace>RootNS.ProjectName</RootNamespace>
</PropertyGroup>

I use Visual Studio Code on MacOSX.

like image 338
Marle1 Avatar asked Jun 19 '18 10:06

Marle1


People also ask

How do I change the default namespace in C#?

Assuming this is for a C# project and assuming that you want to change the default namespace, you need to go to Project Properties, Application tab, and specify "Default Namespace". Default namespace is the namespace that Visual studio sets when you create a new class.

What is the default namespace C#?

The root namespace is also the default namespace for C# programs.

How do you change your namespace?

Place your cursor in the namespace name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Change namespace to <folder name>.

Which is the root namespace of .NET framework?

The System namespace is the root namespace for fundamental types in . NET. This namespace includes classes that represent the base data types used by all applications, for example, Object (the root of the inheritance hierarchy), Byte, Char, Array, Int32, and String.


1 Answers

The answer is going to depend upon the C# plugins you happen to be using with Visual Studio. I've used the <RootNamespace>RootNS.ProjectName</RootNamespace> in the past but it did not work on both Windows and Linux for me. I was using one of the many c# Extensions plugins. Not sure which on.

I am using VS Sharper for C# plugin now. It has a configuration file called "vssharper.json" where you can add things like

{
    "namespace": "Answers.Hello"
}

That works for me in Windows and Linux. I'd try that on your Mac.

like image 107
GlennSills Avatar answered Oct 07 '22 14:10

GlennSills