views:

343

answers:

9

Scenario: I needed to add HttpUtility to my project, and I started by adding "using System.Web" to my collection of using directives. However the HttpUtility class would still not resolve, and I discovered (via this question) that I needed to add a reference to my project.

Question: Why do I need to add a reference to this library when for most other classes a "using" directive will suffice?

+2  A: 

Because some namespaces are spread across assemblies.

The assemblies for the most commonly used namespaces in .NET are added automatically to your project but if your project doesn't already have a reference to the assembly, then you have to add the assembly reference.

Justin Niessner
Thanks for the contribution.
JYelton
+8  A: 

Because Visual Studio adds references to a number of common dlls when creating a new project.

EDIT: To clarify, there are two issues here - namespaces and assemblies. Namespaces represent a logical hierarchy of classes and assemblies are physical 'containers' of a collection of classes. An assembly can contain multiple namespaces and a namespace can be spread accross multiple assemblies (although this is fairly uncommon). The using directive means you don't have to fully qualify a type name e.g. you can declare List<T> rather than System.Collections.Generic.List<T>.

Visual studio adds references to various assemblies when creating a new project which contain a number of commonly-used namespaces such as System. If you add a using directive for a namespace contained within these assemblies then it will work, however if you need to use a namespace contained in a different assembly such as System.Web then you'll need to add the reference before the namespace can be resolved.

Lee
So a new project will have a handful of commonly-used classes, and any others require you add the reference before the using statement becomes useful?
JYelton
@JYelton Correct. There are two concepts here, namespaces and assemblies. An assembly contains the class, and that class is labeled with a namespace. Adding the reference to the assembly, some of which are already added by default, gives you access to the class, such as System.IO.TextWriter. But you must fully qualify the name with the namespace as System.IO.TextWriter. To shorten this you then add the using statement. So really the using statement does nothing more than save you the step of typing "System.IO" everytime you want to use a class in that namespace.
AaronLS
@JYelton - it will have a handful of commonly-used *dlls* (assemblies). Each dll can contain hordes of classes.
Marc Gravell
@JYelton - Yes - if you create a new project in VS and expand the 'references' node in solution explorer you'll see a number of assemblies are already referenced. The using directives you have been using so far reference namespaces that exist in these assemblies, but System.Web does not so you need to add a reference to the appropriate dll(s).
Lee
Thanks all, I've been using VS for a while and apparently most of the classes I've needed were already referenced. You can tell I'm still a newbie!
JYelton
+2  A: 

Any type you use in your application lies in some namespace which lies in some assembly. Using statement only allows to use types without specifying the namespace it belongs to. If you haven't added assembly reference to the project you cannot use its types. The reason why you can use using with some namespaces in that several assemblies' references are included in each project by default.

Andrew Bezzub
+2  A: 

the using statement is just a shortcut to remove the need to define the full namespace in code, you need to add a reference to the library that actually includes the class you want to instantiate.

namespaces are spread across many libraries

Pharabus
+2  A: 

using System.Web is a using directive, not a using statement. But you need to add a reference because the System.Web.dll is not one of the standard dlls included in a winform / wpf etc project. This is in part because it is unlikely you will need it, and in part because it isn't supported on "client profile".

This is really just a library management issue; the CLR is huge; it doesn't assume you want everything.

Marc Gravell
Which is good, I wouldn't want a massive library included in some small utility program. It makes sense, thanks!
JYelton
+25  A: 

Question: Why do I need to add a reference to this library when for most other classes a "using" statement will suffice?

using never suffices, you always need to add a reference to the relevant DLL that contains the class.

But some libraries are referenced in your project by default – most importantly among them mscorlib.dll. Apparently all classes you’ve used until now were in this library.

Konrad Rudolph
This was clearly explained, thank you.
JYelton
I should note that I have added some third-party classes, which I've referenced then added using statements before. I somehow had a mental block when I went to use HttpUtility and assumed it would already be available, or that "using" alone somehow would make it resolve.
JYelton
+1  A: 

In lay terms, "using" helps the compiler know what all needs to be pulled in before it can compile your file. Once it's known what needs to be pulled, it must be found so that it can be pulled in. That's why the reference matters.

psychotik
+7  A: 

The using directive is simply for syntactic simplicity. I.e., instead of having to write System.Web.HttpUtility, you can put using System.Web; at the top of your module and just write HttpUtility.MethodName. However, the reference to the library is what actually allows you make calls to the classes and methods in that library.

Thomas
+1 Great explination Thomas!
Robert Williams
+1 seconded, this made the concept very clear.
JYelton
+1  A: 

Reference means that you're adding a library as dependency to your project. System.Web is it's own DLL file.

Using means that you're locally importing a Namespace or class from your references.

Bobby