Search code examples
c#visual-studiovisual-studio-2012usingusing-directives

How I can know which using-directives are missed?


If a using-directive is missing, is it possible that visual studio says me which using-directives are missing?

And if it is, can I configure my Visual Studio to set automatically the missing using-directives?

Visual studio tells me in the error-list only that any using-directive is missed, but I want to know which is missed?


Solution

  • Visual Studio cannot know what using directives are missing, it can only guess. The same type name can be present in multiple namespaces and assemblies.

    When it guesses it will look for the type(s) you are using and search referenced assemblies for the type. (Tools such as Resharper can guess better/more)

    If the editor shows a blue rectangle below the type:

    obligatory hand drawn arrow

    , put the cursor in the type name and hit Shift+Alt+F10 or Ctrl+. (the shortcut for View.ShowSmartTag ) and a popup will allow you to select from the guesses. Of course you can right-click the type and use the context menu's Resolve or try and click the blue rectangle (RSI risk).

    If the assembly is not referenced you will need to set the reference first. This means that you need to find out what assembly contains the type that you want to use. If it is a type from the .NET Framework you can use the MSDN; on the page that contains the type it will show you what assembly and what namespace the type resides in.

    For instance:

    IEnumerable Interface

    .NET Framework 4.5

    Exposes the enumerator, which supports a simple iteration over a collection of a specified type. To browse the .NET Framework source code for this type, see the Reference Source.

    Namespace: System.Collections.Generic

    Assembly: mscorlib (in mscorlib.dll)