问题
Is it possible to collapse blocks of the code (like "if ... endif") in Visual Studio 2010 as it can be done with methods?
回答1:
yes, it is. Select the code, then hit Ctrl+M,Ctrl+H
(in the default config)
alternatively, right-click and go to Outlining -> Hide Selection.
Here's MSDN on this very feature, which apparently is not available for Visual Basic code.
回答2:
As others have said, this will allow you to collapse a section of code visibly in C#.
#region Name of region
#endregion
However regions are often not used in practice
Why? If a method has so much code in it you need to use regions then your methods are likely too large and you should consider splitting it up into smaller more managable methods.
回答3:
Use following
#region
//Your code which you want to collapse
#endregion
回答4:
The IDE doesn't inherently enable code-folding on structures within methods - but #Region
/ #End Region
achieve this.
回答5:
No, to my knowledge, both language-specific conditional statements (C#, VB etc.) cannot be collapsed*, and just in case this is what you mean (I can't tell due to poor tagging), compilation conditionals can't be, either (other than when one condition isn't met).
You can use the 'Outlining' context menu to get numerous options for handling this kind of thing, though - no nice UI toggling stuff**.
You can 'regionate' your code, but this is easy to abuse.
*At least not 'by standard' or without some third party extension I don't know about.
**Until outlines are made, then you have UI features.
来源:https://stackoverflow.com/questions/15070327/is-this-possible-to-collapse-if-then-else-as-methods