Tuesday, October 9, 2007

Add Using Statement in Visual Studio 2008

Update: Apparently this has been available since VS2005. Who knew? Other than the people that commented, of course. Certainly not me. :)

 

Update #2: So, the "organize usings" menu item that I happened to show in the screenshot alphabetizes the using statements. Also very nice, although I'd like it better if it didn't remove the line breaks I put between groupings. Still, nice. You can also remove unused using statements. Here's the menu:

 


 

Jon Flanders showed this to me the other day. In Visual Studio 2008, if you've just added a class for which you haven't yet added the corresponding using statement to bring the namespace into scope, you can hit control-dot, and up pops the following menu:

 


 

And sure it enough it pops a "using System.Collections;" in up at the top of the file. Very nice. Better still if it would put it in alphabetical order, but I seem to recall that the order of using statements is significant when Linq is in play, so maybe they couldn't do that.

 

You can also access it via the right-click menu:

 

6 comments:

  1. That feature is actually in VS2005 also. One of my favs.

    ReplyDelete
  2. Also works in VS 2005, for those of us not sufficiently cool to be using 2008... The menu is a little different.



    And what's the "Organize Usings" menu do?

    ReplyDelete
  3. As usual, Kevin knows all. :)



    Herb: I've updated the post to describe the menu. Thanks for asking - I never would have noticed it otherwise.

    ReplyDelete
  4. I think the "Organize Usings" is new in 2008 as I haven't been able to find it in 2005. What I'd love to see "Organize Usings" is collapse them into smaller whitespace:



    using (Foo f = new Foo())

    {

    using (Foo g = new Foo())

    {

    }

    }



    would become:



    using (Foo f = new Foo())

    using (Foo g = new Foo())

    {

    }

    ReplyDelete
  5. Yeah, except leaving off your braces is the work of the devil. ;)



    ReplyDelete
  6. Yes, "sort/remove usings" is new in VS2008 and the "resolve" was in VS2005 for C#. In case there are any VB devs lurking here, this option is now available in VS2008 for VB devs too.

    ReplyDelete