Is there any way to fix "using" in my C# file? I looking for some sort of "fix (organize) imports" from NetBeans or Eclise for java. I looking for the easiest way to fix issue: "The type or namespace could not be found" when I miss some "using" directive. Actually, I do not remember all C# classes hierarchy.
2 Answers
When you type the name of a type which isn't already imported, it will be highlighted in red. If you press Ctrl-period, Visual Studio will offer to import it. (There should be a blue dot near the cursor to indicate this as a possibility.) Or using ReSharper, press Alt-Enter on the line with the problem.
Note that that only tackles the "adding a using directive" side of things. There's also the "Remove and sort using directives" command for keeping things tidy. I typically having that bound to Ctrl-Shift-U, but I don't think it has a shortcut by default.

- 1,421,763
- 867
- 9,128
- 9,194
-
But does it mean, there is no way to add all the appropriate using directives (asking the user to choose if there is ambiguity) for the whole file at once? I cannot find it. – Jirka Jan 23 '13 at 17:16
-
@Jirka-x1: I'm not aware of one, no. Resharper *may* have something along those lines. I usually find it's fine to just fill them in as I go. – Jon Skeet Jan 23 '13 at 17:22
If you have the necessary assemblies referenced, you can right click (or hover) on an unrecognized class name to find a "Resolve" submenu to help you. You can find options to add necessary usings or change the class name to fully qualified with namespaces in your code.

- 76
- 5