Visual Studio C# statement collapsing

C#Visual Studio

C# Problem Overview


When editing really long code blocks (which should definitely be refactored anyway, but that's beyond the scope of this question), I often long for the ability to collapse statement blocks like one can collapse function blocks. That is to say, it would be great if the minus icon appeared on the code outline for everything enclosed in braces. It seems to appear for functions, classes, regions, namespaces, usings, but not for conditional or iterative blocks. It would be fantastic if I could collapse things like ifs, switches, foreaches, that kind of thing!

Googling into that a bit, I discovered that apparently C++ outlining in VS allows this but C# outlining in VS does not. I don't really get why. Even notepad++ will so these collapses if I select the C# formatting, so I don't get why Visual Studio doesn't.

Does anyone know of a VS2008 add-in that will enable this behavior? Or some sort of hidden setting for it?

Edited to add: inserting regions is of course an option and it did already occur to me, but quite frankly, I shouldn't have to wrap things in a region that are already wrapped in braces... if I was going to edit the existing code, I would just refactor it to have better separation of concern anyway. ("wrapping" with new methods instead of regions ;)

C# Solutions


Solution 1 - C#

Starting with Visual Studio 2017, statement collapsing is built-in.

There are several extensions that perform this task for pre-2017 versions of VS, starting with VS 2010 version:

The last extension supports only VS 2015 and VS 2017, but it's the most powerful one.
It supports syntax coloring inside collapsed blocks, it is more fault-tolerant and optimized.

>If the extension doesn't seem to install after you used a browser to download it, try using the built-in Visual Studio extension manager.

Solution 2 - C#

I'm not aware of add-ins, but you mentioned regions and I see nothing wrong with doing something like this...

foreach (Item i in Items)
{
  #region something big happening here
  ...
  #endregion

  #region something big happening here too
  ...
  #endregion

  #region something big happening here also
  ...
  #endregion
}

EDIT: In response to the question's EDIT: You're right, sticking a bunch of regions everywhere isn't ideal and refactoring is probably the way to go. But it seems that you're looking for something magical that will "organize" the code for you, and I don't think that exists.

Solution 3 - C#

You can collapse specific blocks of text within visual studio, but you have to turn off automatic outlining.

Right click in your code window and select (Outlining | Stop Outlining)

Then, select some text, right click and select (Outlining | Hide Selection)

When you turn on automatic outlining again, your custom "Regions" will no longer collapse.

Solution 4 - C#

Visual Studio 2008 supports regions inside of functions as long as you keep them in the same code hierarchical level

#region Won't work
for(int i = 0; i<Count; i++)
{
//do something
#endregion
}

for(int i=0; i<Count; i++)
{
#region Works fine
//do lots of stuff
#endregion
}

Solution 5 - C#

Let me say something different: press(ctrl+m,ctrl+h) or in edit>outlining>hide selection its so useful.

Solution 6 - C#

This feature has been added to Visual Studio 2010's C# editor. I can't find the source verifying it was actually put in, but I remember seeing it on one of the Dev 10 team member blogs talking about changes since Beta 1 or something. As a consolation, here's one Microsoft comment suggesting they wanted to add it.

Solution 7 - C#

I will add here that in VS 2010 Microsoft has added WPF adorner capabilities using Managed Extensibility Framework (MEF), this will allow us to extend the source code editor to organize them in a much better way to make it more readable and accessible.

For instance the Summary Comments visualizer that Scott Gu demoed at PDC 2008.

So look forward to a better tomorrow for developers :)

Solution 8 - C#

Coderush will outline all code blocks for you. Not sure if it allows you to expand/collapse the blocks, but outlining is the next best thing. I use resharper instead of coderush which as far as I know doesn't provide block collapsing either :(

Solution 9 - C#

I have found this for Visual Studio 2013 and found it very helpful. It works even if you put simple braces around your code with { ..... }

After sharing I found somebody else also mentioned this link. My vote is for this tool also.

C# Outlining Tool for Visual Studio 2013

Solution 10 - C#

In VS2017 you can highlight a section of code, right-click, Outlining > Hide selection. This will collapse the code and provide a toggle to the section highlighted.

Solution 11 - C#

In Visual Studio 2019, if you want to collapse braces in catch & finally, collapse switch, case, default, collapse multiple lines of comments, etc.

Try to use C# outline 2019

Solution 12 - C#

region ,#endregion is the smart option.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrankView Question on Stackoverflow
Solution 1 - C#SkybladeView Answer on Stackoverflow
Solution 2 - C#KonView Answer on Stackoverflow
Solution 3 - C#Matt BrunellView Answer on Stackoverflow
Solution 4 - C#Chris MarisicView Answer on Stackoverflow
Solution 5 - C#M atView Answer on Stackoverflow
Solution 6 - C#Sam HarwellView Answer on Stackoverflow
Solution 7 - C#VinView Answer on Stackoverflow
Solution 8 - C#goku_da_masterView Answer on Stackoverflow
Solution 9 - C#BuilderView Answer on Stackoverflow
Solution 10 - C#LoflinAView Answer on Stackoverflow
Solution 11 - C#逍遥子kView Answer on Stackoverflow
Solution 12 - C#biswaView Answer on Stackoverflow