次の方法で共有


Cleaning up 'using' directives

A suggestion arrived in email, and here is my response. (None of this is on the feature list for Whidbey, it’s just ideas.)

1. Fully qualify (all names / all names in namespace X / all instances of type Y / the instance under my cursor), removing a using directive if possible.

1a. Add 'global::' when doing this.

2. Least-qualify (one of the above) by removing namespace from the name and adding a using directive.

3. Partially-qualify names from stuff within this project. That means writing like this:

 

using System.Foo;

using System.Bar;

namespace Microsoft.Product.Feature.ComponentA

{

     class C {}

}

namespace Microsoft.Product.Feature.ComponentB

{

     class D

     {

          ComponentA.C x;

     }

}

 

4. Find all unused using directives (one you can remove without changing the meaning of the code). A little tricky if you use #if, so avoid it.!

5. Sort using directives alphabetically. Insert a blank line to separate different top-level namespaces.

Comments

  • Anonymous
    May 10, 2004
    I personally like Java's take better, when you cna specify types to be imported, not whole namespaces. Looks like Microsoft once again decided to go with developers who do not want to be bothered with listing the actual types they use (even though Java allows you to import the whole namespace as well). And of course the "Organize Imports" feature in Java IDEs makes life a lot easier, not sure if VS has anything like that.

  • Anonymous
    May 10, 2004
    All sound good to me.

  • Anonymous
    May 15, 2004
    The comment has been removed

  • Anonymous
    May 20, 2004
    Is there currently anyway to remove unused using statements?

  • Anonymous
    May 25, 2004
    The Xtreme Simplicity Refactoring plug-in for VS.NET has a "Tidy Imports" feature that will clean up unused imports.

  • Anonymous
    May 25, 2004
    Thanks, Scott. Do you use it? A lot? Does it meet your needs?

  • Anonymous
    June 01, 2009
    PingBack from http://uniformstores.info/story.php?id=15257

  • Anonymous
    June 02, 2009
    PingBack from http://uniformstores.info/story.php?id=29246