Condividi tramite


Organize Usings (C# Only)

Menu:  Edit -> IntelliSense -> Organize Usings -> [Remove Unused Usings, Sort Usings, Remove and Sort]; RIGHT CLICK -> Organize Usings -> [Remove Unused Usings, Sort Usings, Remove and Sort]
Command:  EditorContextMenus.CodeWindow.OrganizeUsings.[RemoveUnusedUsings, SortUsings, RemoveAndSort]
Versions:  2008,2010
Published:  6/29/2010
Code:  vstipEdit0070

 

Got issues with your Using statements?  Need a little help getting them organized?  No problem!  You can simply RIGHT CLICK anywhere in the editorto get the Context Menu then choose Organize Usings; you should see this:

image

 

Remove Unused Usings - removes unused using directives, using aliases, and extern aliases.  There are a couple of things to note about this feature.  First, it should ONLY be use on code that builds as it could remove required usings if activated on code that does not build.  There is an option in VS2008 (NOT VS2010) found at  Tools -> Options -> Text Editor -> C# -> Advanced -> Organize Usings that will prevent you from removing usings if you code doesn't build:

image

 

Second, it only works on the active set of code.  So if I have a set of usings:

image

 

And remove the unused usings; this is what I get:

image

 

However, if those usings are organized into code that has active and inactive blocks:

image

 

Then only extra usings in the ACTIVE block will be removed:

image

 

 

 

Sort Usings - will sort in this order: extern aliases, using directives, using aliases.  Also, by default, it will sort usings that reference the System namespace before other using directives.  So if we again have some usings:

image

 

And sort them, we get this:

image

 

Notice the Microsoft namespace is below all the System namespaces.  If you don't like this behavior, you can change it by going to Tools -> Options -> Text Editor -> C# -> Advanced -> Organize Usings and deselecting the "Place 'System' directives first when sorting usings" option:

image

 

This is what you will get as a result when you sort:

image

 

 

 

Remove and Sort - Performs a remove operation then a sort operation.

Comments

  • Anonymous
    June 29, 2010
    There is no need to select the using statements, just right click in the editor and make use of the "Organize[sic] Usings" sub-menu.

  • Anonymous
    June 29, 2010
    Yep.  Good catch :)  I've updated the tip to reflect the change. Z

  • Anonymous
    June 29, 2010
    Why does VB.NET STILL not have a comparable feature? How hard can it be do this with "imports"? And also, why on earth would the "warn if build errors exist when organising usings" be removed in VS2010???

  • Anonymous
    June 29, 2010
    Excellent points.  I've pinged the teams to find out the "why" behind some of this stuff... Z

  • Anonymous
    July 20, 2010
    I want to make this in all my solution, is there way? Or the only way is do this in each class? :( Thank you.

  • Anonymous
    July 20, 2010
    ah, I know with Macros it's possible. But I wanted it in this way.. Thanks!

  • Anonymous
    July 25, 2010
    @Yann -- forgot to mention that the VB team indicated that they will probably be coming out with a feature like this for VB that will be available in the Power Tools for VB.  Not sure of any specific timeline but looks like it is definitely on their radar. Z

  • Anonymous
    July 25, 2010
    @Gabriel--to my knowledge there is no univeral way to do every class at once without a macro. Z

  • Anonymous
    July 25, 2010
    @Yann -- Looks like the build errors feature will not be coming back.  Here is what I got from the team: "We ended up removing this option because it turned out that it wasn’t entirely accurate 100% of the time and there were perf implications of doing the analysis for build errors. " Z