Visual Studio C#: Why is a "using" directive insufficient for some libraries?
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 开发者_JAVA技巧a "using" directive will suffice?
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.
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.
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.
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.
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.
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
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.
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.
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.
精彩评论