Is this possible to collapse “If Then Else” as methods?

倖福魔咒の 提交于 2020-01-13 19:48:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!