17

Visual Studio indents code within namespace. This can be avoided when disabling indentation globally, which is not what I want. In all other cases, the indentation is fine, I simply don't like the fact that all code is one level - this makes it look ugly to me.

namespace X
{
    public class A
    {}
}

I would prefer it like this:

namespace X
{
public class A
{

}
}

In C++, there's a nice workaround as explained here:

namespace X
{; // the ; after opening brace makes visual studio not indent the class below.

class A
{};

}

But in C#, a namespace cannot directly contain fields so this doesn't work.

How can I get Visual Studio to stop indenting namespaces without disabling indentation globally?

Update Visual Studio 2013 behavior C++ has changed

Tools->Options->C/C++->Formatting->Indentation: [ ] Indent namespace contents 

enables my preferred formatting, while the {; trick doesn't work anymore. But no change for C# that I could find.

Community
  • 1
  • 1
Wilbert
  • 7,251
  • 6
  • 51
  • 91
  • Select all code inside namespace braces and press `Shift + TAB`. Job done! :-) – Belogix May 22 '13 at 08:53
  • That won't work when formatting the document. – Gorgsenegger May 22 '13 at 08:54
  • 3
    I was only joking / being sarcastic! This seems like an odd question as I have NEVER worked anywhere that would accept this code format! Most modern monitors are wide-screen and really wouldn't make much difference with a leading tab or not! – Belogix May 22 '13 at 08:55
  • 1
    I also wondered why one would want to do such a thing and have the code formatted in a way I've never seen before. Anyway, if there is a way and it makes @Wilbert happy so be it - I don't know any though :) – Gorgsenegger May 22 '13 at 08:58
  • 5
    It's a personal preference, but I do not seem to be the only one with that preference. And yes, a solution to this would make me happy. – Wilbert May 22 '13 at 09:02
  • There's another thread about this: http://stackoverflow.com/questions/2225487/way-to-get-vs-2008-to-stop-forcing-indentation-on-namespaces – RenniePet May 11 '16 at 21:20
  • 1
    And yes, it's a personal preference, and I wish there was a good solution. What inspired me to want to do this is that I sometimes copy bits of code, for example a single method, from C# to Java/Android. The result needs to be edited of course, but not having to remove the extra indentation would be one less editing step. – RenniePet May 11 '16 at 21:24
  • 2
    Anyone who's interested in this (and it has been viewed over 500 times) please vote here to try to get Microsoft to provide this option: https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/6367724-add-option-to-prevent-indentation-of-classes-insid – RenniePet May 14 '16 at 14:40

2 Answers2

10
  1. Text Editor → c# → Tabs → Indenting — Set to "Block"
  2. Text Editor → c# → Formatting → General — Turn off every checkbox saying "automatically format..."
cesarito
  • 136
  • 2
  • 5
  • 4
    Please say what the checkboxes are, instead of "first two". It would help in case VS changes its settings dialogs. And possibly remove Tabs in the second, it's misleading. – Jarekczek Oct 14 '16 at 08:52
7

This question deserves a new answer in 2021. C#10 (shipped with .NET 6 and Visual Studio 2022) introduces File Scoped Namespaces. It allows the following syntax:

namespace Hello.World;

class MyClass
{
    public static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

So, we can completely remove indentation added by the namespace declaration. The old syntax is supported too.

Paweł Bulwan
  • 8,467
  • 5
  • 39
  • 50