Can I collapse foreach, using and other c# code blocks in Visual Studio 2010, the same way that I can collapse methods, properties, classes, namespaces, etc? It would be very helpful sometimes.
views:
57answers:
3You can use #region #endregion
directives to create collapsible areas.
Update:
By the way, why do you need this functionality? There's good a principle to keep your methods as short as possible (Uncle Bob calls it extract until you drop
). If you stick to it you won't need additional collapsible areas.
Cut + paste the code out of the foreach loop and into a function, collapse the function away?
E.g.,
public void MyMessyFunction()
{
// ...
foreach(string foo in bar)
{
// Do ugly stuff
}
// ...
}
Turns into
public void MyMessyFunction()
{
// ...
foreach(string foo in bar)
{
DoUglyStuff(foo, bar);
}
// ...
}
#region Stuff I want to hide
public void DoUglyStuff(string foo, List<string> bar)
{
// Do ugly stuff
}
#endregion
As I recall, you can collapse an arbitrary region in Visual Studio 2010. Try selecting a region, then press Ctrl+M twice (assuming C# key bindings).
In VS2008, this is found under Edit -> Outlining; I don't have a 2010 installed so can't try it out there readily.
Edit: Actually, it looks like VS2008 has something similar in its "Hide Selection" feature (found in the same place), but it appears to only be available in some languages.