How to collapse If, Else, For, Foreach, etc clauses?

66,681

Solution 1

Try this plugin (C# Outline Extension):

Solution 2

Visual studio IDE gives an option to outline the if, for, foreach, while, switch case etc.

Just go to Visual Studio -> Tools -> Options -> Text Editor -> C\C++- > View and set Outline Statement Blocks to true.

In Visual Studio 2012+, the path is Visual Studio -> Tools -> Options -> Text Editor -> C\C++- > Formatting.

Solution 3

I use 2 Extensions:

C# outline 2013

and

Indent Guides

Solution 4

select the code you want to collapse and then press ctrl+M,ctrl+H. to disable it, press ctrl+M,ctrl+U, you can also right click on the selection, and there "Plan Mode" ( I'm not sure my VS is french )

Solution 5

You can use #region for collapsing if, else, for and other similar clauses.
Example:

#region foreach
foreach(/*conditions*/) 
{
    statement 1;
    statement 2;
    ..
    statement n;
}
#endregion

Now for collapse all use the shortcut ctrl+M,ctrl+O.

Share:
66,681
Moslem Ben Dhaou
Author by

Moslem Ben Dhaou

With an Applied Computer Science engineering degree, I have been working with C#, ASP.NET and SQL Server for over 10 years. Currently, an IAM Consultant. I have a 15+ years overall of programming experience.

Updated on July 05, 2022

Comments

  • Moslem Ben Dhaou
    Moslem Ben Dhaou almost 2 years

    I get stuck sometimes with very long clauses and I am looking for a way that allows me to collapse them, same way as I can collapse classes, methods and namespaces by default.

    Is there a Visual Studio extension that does that? neither ReSharper nor JustCode does allow it.

    Thank you!

  • Moslem Ben Dhaou
    Moslem Ben Dhaou about 10 years
    +1 Thank you for that answer, unfortunately I checked and nothing similar is available for the C# editor, therefore I cannot accept the answer. Thanks!
  • dtmland
    dtmland almost 10 years
    While it applies to a different language, this answer still needs more love.
  • gitsitgo
    gitsitgo almost 10 years
    Man, this is perfect. Makes me wonder how MS ignored such essential code editing functionality.
  • Roshdy
    Roshdy almost 9 years
    I installed it, then restarted VS 2013, but it didn't work, any suggestions?
  • Tim Partridge
    Tim Partridge almost 9 years
  • Ahmad Ghadiri
    Ahmad Ghadiri over 8 years
    That is pretty cool, specially for people who are using Visual Studio Express hence they can't use plugins, thanks!
  • mkb
    mkb almost 8 years
    VS2015 still need this extension
  • mkb
    mkb almost 8 years
    I don't think this a nice option, regions are there because code is hard to manage and move around, and since they don't make code look good enough I use this extension: I Hate #Regions
  • Piotr Knut
    Piotr Knut over 7 years
    It's work. I use it before but I forgoten name this extenssion. Thank's.
  • 逍遥子k
    逍遥子k over 3 years
    And there is C# outline 2019 for Visual Studio 2019 now.
  • Johan Boulé
    Johan Boulé almost 3 years
    Rename you .cs file to .cpp and you can do this.