How to collapse If, Else, For, Foreach, etc clauses?
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:
and
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.
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, 2022Comments
-
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 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 almost 10 yearsWhile it applies to a different language, this answer still needs more love.
-
gitsitgo almost 10 yearsMan, this is perfect. Makes me wonder how MS ignored such essential code editing functionality.
-
Roshdy almost 9 yearsI installed it, then restarted VS 2013, but it didn't work, any suggestions?
-
Tim Partridge almost 9 years2013 version: visualstudiogallery.msdn.microsoft.com/…
-
Ahmad Ghadiri over 8 yearsThat is pretty cool, specially for people who are using Visual Studio Express hence they can't use plugins, thanks!
-
mkb almost 8 yearsVS2015 still need this extension
-
mkb almost 8 yearsI 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 over 7 yearsIt's work. I use it before but I forgoten name this extenssion. Thank's.
-
逍遥子k over 3 yearsAnd there is C# outline 2019 for Visual Studio 2019 now.
-
Johan Boulé almost 3 yearsRename you .cs file to .cpp and you can do this.