简体   繁体   中英

Can I collapse foreach, using and other c# code blocks in Visual Studio 2010?

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.

You can use #region #endregion directives to create collapsible areas.

Update :

By the way, why do you need this functionality? There's a good 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?

Eg,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM