Resolve Namespace (Visual Studio, Resharper)

How do you "autogenerate" a "using" namespace for a class you typed in Visual Studio? Read along… [more]

I've seen a couple of presentations (in-person, online et al), webcasts etc where there the presenter typed a class name, highlighted by Visual Studio because the namespace cannot be resolved (no using directive or class was not prefixed by the correct a namespace – fully qualified name if that's the right term – though I think it applies to assemblies only).

Anyways, most if not all the time, what they do is add and type the using "directive" (not sure if directive is the right terms, if not then you may call it "usings") manually. There could be a some benefits this in presentations, like making the point clear that the namespace must be added (and what namespace it is). But if the audience are intermediate to advanced level, I think we can do away with it.  However, even in those cases, most are still doing it by typing manually.

I have nothing against this of course but for those who aren't that well versed enough of the numerous namespaces out there, the "resolve" feature in Visual Studio might save you a little time spent coding rather than finding which namespace it is in. Note however, that this only works if the DLL/assembly containing the namespace is referenced in the project. And the spelling and case sensitivity of the class is correct (at least for C#, not sure for VB). Even then, I'm sure a number of us knows the exact class name but not very certain about the namespace (or maybe we just want to save a little time, there's nothing wrong with that).

So to resolve the namespace in Visual Studio (2005, not sure about 2003 yet, but for sure 2008 has this too), here are the steps: (using StringBuilder as the class for this example)

1. Type the class name (make sure you type at the appropriate place/usage – otherwise, you might not be able to resolve properly if you have syntax errors around that area)

2. Right Click on the typed class name

3. context menu should appear and you should see a resolve context menu item (like the one below) 

4. Under the resolve context menu, you generally have two options. The one at the top (henceforth called "upper" option) will create a "using" directive on top of the you class file (something like what's in the image below)

Or if you choose the "lower" option, you Visual Studio would prefix your class with the correct namespace (if you have a reason not to use "using"). 

Also I did mention "generally two items". This is because there can be
more than two. Say if there are classes (with the same name) belonging
to more than one namespace. Each namespace will have a corresponding
menu item for the "upper" and the "lower" options.

So that's all ther is to it.

Finally, if you have Resharper (using v3.1 – and if you don't I suggest you give it a try) then when you hover over the class with the missing namespace you should something like:

Or as indicated in the menu that popped up, you can also place your cursor on the class then press Alt + Enter and the using directive will be added (not sure how to come up with the "lower" option in R#).

So there you have it. I believe one of the underutilized, simple yet productive features of Visual Studio. I suggest you try and use it and I wonder what reasons could make me think otherwise. If there is any.

Smile

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.