11

I would like to generate a new C# Class or C# Interface in Microsoft Visual Studio Code following the newest C#10 file-scoped namespace syntax.

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces

Beginning with C# 10, you can declare a namespace for all types defined in that file, as shown in the following example:

namespace SampleNamespace;

class AnotherSampleClass
{
    public void AnotherSampleMethod()
    {
        System.Console.WriteLine(
            "SampleMethod inside SampleNamespace");
    }
}

I'm generating C# classes this way:

Right click on folder in the explorer -> New C# Class.

The output looks like this: (the old syntax with curly braces)

namespace SampleNamespace
{
    class SampleClass
    {
        public void SampleMethod()
        {
            System.Console.WriteLine(
                "SampleMethod inside SampleNamespace");
        }
    }
}

I'm using C# for Visual Studio Code (powered by OmniSharp). v1.24.0

VS Code version is 1.62.3

Is there any way to override the generator behaviour to generate new file-scoped namespace syntax?

Mikolaj
  • 1,231
  • 1
  • 16
  • 34
  • Does this answer your question? [VS 2022 - Convert to file-scoped namespace in all files](https://stackoverflow.com/questions/69889519/vs-2022-convert-to-file-scoped-namespace-in-all-files) – jazb Feb 02 '22 at 06:53
  • 4
    Just delete the curly bracket after the namespace, and at the file end, put a semicolon after the namespace and un-dent the whole file (Ctrl-a, shift-tab)? – Caius Jard Feb 02 '22 at 06:56
  • 2
    @CaiusJard I'm lazy :), I don't want to repeat these steps every time I created a new class. – Mikolaj May 06 '22 at 10:37
  • "It's not lazy, it's efficient" – Caius Jard May 06 '22 at 14:43
  • Have you found a solution? – MrDave1999 Jul 11 '22 at 18:35
  • @MrDave1999 No :/ – Mikolaj Jul 12 '22 at 07:09
  • 1
    There is an extension called [csharpextensions](https://github.com/jchannon/csharpextensions) (no longer maintained), however, you could add a new template [here](https://github.com/jchannon/csharpextensions/tree/master/templates) with file-scoped namespace sintax. I'm not very good at JavaScript :(, otherwise I would make a fork and publish a new extension with that feature. – MrDave1999 Dec 09 '22 at 03:13

4 Answers4

29

Sorry this is a late response but I've just been at this myself.

What you're looking for is a Visual Studio setting.

Go to: Tools > Options > Text Editor > C# > Code Style

The 4th block down, "Code block preferences"

Change "Namespace declarations" from "Block scoped" to "File scoped"

(This is in Visual Studio 2022)

EDIT (Dec 22nd):

For VS Code, I have installed "C# Extensions" by JosKreativ

Once installed, go to: File > Preferences > Settings

In the "Search settings" textbox type "namespace"

You'll see the option (for me 3rd);

"Csharpextensions: Use File Scoped Namespace"

Check that checkbox and you're good to go.

Boomer
  • 358
  • 3
  • 11
  • 1
    This is a great instruction for Visual studio but the question was how to implement it in Visual Studio Code. – rebecsan Nov 22 '22 at 14:50
0

If you don't get the answer you seek in terms of changing the generator, you could consider:

  • Install AutoHotKey
  • Add this to the AHK script:
; CtrlAltN - convert to file scoped namespace
^!N::
KeyWait Control
KeyWait Alt
Send ^{Home}{Down}{Delete}{Delete}^{End}{Backspace}^a+{Tab}
return
  • Make a new class
  • Put the cursor anywhere in it and press Ctrl+Alt+M

It literally presses the following keys (after waiting for you to release Ctrl, Alt)

  • Ctrl+Home - cursor to top of file
  • Down - cursor to { line
  • Del Del - remove { and line
  • Ctrl+End - go to end
  • Bksp - remove }
  • Ctrl+a - select all
  • Shift+Tab - undent

I'm sure you can tweak the script as required; ; prefixing a line is a comment, ^!N:: is a hotkey ^ means Ctrl, ! means Alt, + means Shift.. Then the other keys are either {named} or literal chars

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

If you look for the snippets created, you will find namespace and fsnamespace (file name namespace).

In short, you should start typing fsnamespace until you get the suggestion and then press enter.

Another option is to create a snippet. To do that, go to Preferences -> Configure User Snippets -> Select if you want it for the language or project and then paste the following:

"Namespace": {
    "prefix": "namespace",
    "body": [
        "namespace ${1:Name};"
    ],
    "description": "Namespace"
},
William Ardila
  • 1,049
  • 13
  • 22
-1

you should go to user preference settings (ctrl + alt + p), search for namespace and check csharpextension: use file scoped namespace. And it will work for all .NET 6 + projects

Andyrub18
  • 1
  • 1