22

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?

7
  • Does this answer your question? VS 2022 - Convert to file-scoped namespace in all files Commented Feb 2, 2022 at 6: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)? Commented Feb 2, 2022 at 6:56
  • 3
    @CaiusJard I'm lazy :), I don't want to repeat these steps every time I created a new class. Commented May 6, 2022 at 10:37
  • "It's not lazy, it's efficient"😀 Commented May 6, 2022 at 14:43
  • 1
    There is an extension called csharpextensions (no longer maintained), however, you could add a new template here 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. Commented Dec 9, 2022 at 3:13

4 Answers 4

49

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great instruction for Visual studio but the question was how to implement it in Visual Studio Code.
1

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

Comments

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"
},

Comments

-2

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

1 Comment

Does it work in vs code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.