Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in visual studio 2022 to add a new line before and after namespace?

I am using Visual Stuido 2022 to code my C# project.

Is there a way to configure VS using (.editorconfig file) where a new line is added before and after the namespace?

So my class will look like this

using System;

namespace ProjectName.Tests;

public class Test
{
    
}

instead of

using System;
namespace ProjectName.Tests;
public class Test
{
    
}

like image 749
Jay Avatar asked Nov 24 '25 10:11

Jay


1 Answers

I'm not sure there is a Visual Studio native way of doing this.

There is definitely not a way to do this in .editorconfig with Visual Studio alone (meaning no plugins). About halfway down Namespace declaration preferences, it talks about csharp_style_namespace_declarations, and the code formatting sample when that value is file_scoped looks like

// csharp_style_namespace_declarations = file_scoped
using System;

namespace Convention;
class C
{
}

which appears to get you part of the way there (blank line after using). When you look at the supported formatting rules, the list is pretty brief.

If you have ReSharper there is a way. These settings in .editorconfig will do what you want:

resharper_blank_lines_after_file_scoped_namespace_directive = 1
resharper_blank_lines_after_imports = 1

If ReSharper is not an option, here are 3 possible paths to take, none all that great. They certainly aren't simple solutions.

  • try to find something in the Visual Studio Marketplace
  • write a .NET Analyzer that is configured via .editorconfig (ref. this page)
  • raise an issue on Developer Community, and hope they get to it.
like image 57
Kit Avatar answered Nov 26 '25 07:11

Kit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!