(Yet another question from my "Clearly I'm the only idiot out here" series.)
When I need to use a class from the .NET Framework, I dutifully look up the documentation to determine the corresponding namespace and then add a "using" directive to my source code:
using System.Text.RegularExpressions;
Usually I'm good to go at this point, but sometimes Intellisense doesn't recognize the new class and the project won't build. A quick check in the Object Browser confirms that I have the right namespace. Frustration ensues.
Using HttpUtility.UrlEncode() involved adding the appropriate directive:
using System.Web;
But it also required adding a reference to .NET Framework Component for System.Web, i.e. right-click the project in Solution Explorer, select Add Reference and add System.Web from the .NET tab.
How might I discern from the documentation whether a .NET namespace is implemented by a .NET Framework Component that must be referenced? I'd rather not hunt through the available components every time I use a namespace on the off chance that a reference is needed.
(For those who like to stay after class and clean the erasers: Will Organize Usings > Remove and Sort also remove references to componenents that are not used elsewhere in the project? How do you clean up unnecessary references?)
Check out this link for UrlEncode:
Namespace: System.Web
Assembly: System.Web (in System.Web.dll)
The Assembly line tells you which dll to reference.
You'll note that the documentation (e.g. http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx) tells you the name of the assembly/DLL that the class should be found in, along with the class's namespace.
Namespace: System.Web
Assembly: System.Web (in System.Web.dll)
On a side note, I know it can be a little dear, but Resharper makes things like this so much easier. If you're a serious developer, you may want to consider investing in a license. For the eraser-cleaners, Resharper adds a handy little "Find Code Dependent on Module" item to the right-click menu on references in the Solution Explorer. It's not quite an automatic cleanup, but it makes it a lot easier to see whether something's still being used by your project.
The documentation specifies two things for any type:
The namespace of the type (for the using directive)
The assembly containing the type (this is what you add a reference to)
To take an example where the two are different, look at the documentation for Enumerable:
Namespace: System.Linq
Assembly: System.Core (in System.Core.dll)
If you look at the MSDN docs, e.g.
http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx
It tells you the namespace and the assembly that is required.
First, a correction to your terminology: What you are referencing is called an "assembly". An assembly contains classes that belong to a namespace. A namespace can span across multiple assemblies.
Most assemblies are named the same as the main namespace that is contained in them. For example, System.Web exists in System.Web.dll. The documentation also usually tells you which assembly needs to be referenced.
I think that you are running into a difference between c# and C here. To compare: In C, all you need to do to include a new library is to include it in the header.
It .net, you need to be aware of 2 things:
A namespace can span more than one assembly/dll (that means that you might not get a compiler error on the using clause, because some of the dlls that support that namespace are referenced -- just not the one that you need)
To "see" the contents of a given assembly, you have to add a reference to it. The using clause alone just gives you some short-cut syntax so that you can write HttpUtility.Encode(), instead of System.Web.HttpUtility.UrlEncode(), you have to add the reference in order for the compiler to "know" about the class.
To avoid your problem:
In the MSDN documents, pay attention to the assembly that the class is in, and make sure that you have a reference to the assembly.
If you browse to the MSDN for the class you're trying to use. It typically tells you the assembly that the class is in. For example the Regex class is in Assembly System (in System.dll) or the HttpUtility class is in Assembly System.Web (in System.Web.dll).
I believe tools such as ReSharper help with this, as well, and automatically references the assemblies you need.
I'm pretty sure you have to manually remove the unused references in C# projects. In VB.NET projects there's a button to list the unused references when you're in the project properties page. I don't see this in C# projects, though.
Related
I removed all references from the References node but I am still able to use System.Collections.Generic namespace. Why? Is it because Visual Studio somehow adds them by default? But if that is the case, I should be able to see it my .csproj file at least.
What's going on here?
The documentation for List<T> says the class is defined in mscorlib. This is automatically implicitly available to every C# project out there. It has to be, because it's the same assembly which defines basic types such as object, void, string, int. You're not going to be able to write C# code without those, that's why the assembly is given special treatment.
There are ways to suppress its inclusion, but that's only useful if you intend to re-implement the whole .NET runtime, you won't be able to use the resulting assembly otherwise.
Namespaces don't necessarily map one-to-one with assemblies. There doesn't have to be a separate System.Collections.Generic.dll or System.Collections.Specialized.dll, for example.
The mscorlib.dll and System.dll assemblies contain most of the "basic" namespaces, and they're implicitly referenced in your C# project.
I'm working on app which uses a lot of external assemblies (newtonsoft.dll, Yahoo Yui compressor.dll, fleck.dll etc). In each c# file I need to add using statement with all those assemblies. Is it possible to create my own assembly (i.e. LIBRARY.dll) containing all the dll's and refer only to this in all c# files?
No. Firstly, using directives refer to namespaces, not assemblies (assembly references are defined at the project level). Secondly: you almost certainly don't actually need all of them in every file. But: you can create a new-file-template with the ones you are likely to need. But frankly it is usually easier to either copy/paste them, or just add them when they are needed. In the IDE, this is as simple as pressing ctrl+.,ret after a type name that doesn't resolve... so MySpecialTypectrl+.,ret should add the missing using directive to resolve MySpecialType.
using does not refer to assembly, but to namespace. So the answer is "no"...
using System; // you are using items in the System namespace
using System.IO; // you are using items in the System.IO namespace
No. Usages of individual types need to be resolved to the namespaces where they are defined in. So, you will still need to include the resolution paths in your usings.
At the top of my program I have the following :
using System.Configuration;
Within my code, I have the following:
int CompanyID = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]
.ToString());
I am getting the following error though :
The name 'ConfigurationManager' does not exist in the current context
I am not sure what I am missing.
To expand a bit, you will need to add a reference to System.Configuration.dll to get this to work. It's kind of misleading because the System.Configuration namespace also exists inside the base System.dll, and holds some far lesser used objects like SettingsContext. As a result, it seems like it really ought to work, but it doesn't. It is really confusing, and currently one of those obtuse gotchas in the .NET framework.
Fortunately, System.Configuration.dll is in the .NET base framework, so you only need to add a reference by right-clicking on the References folder in your project, clicking Add Reference, and then finding System.Configuration under the .NET tab.
After it has been imported into your project, don't forget to add using System.Configuration to the top of the code file you intend to use ConfigurationManager in.
You need to add a reference to System.Configuration in your project.
I am working on an assignment that specified "Do not use any external libraries". So I created a c# application, and the first thing I did was remove all the dll's references by default... including "System.dll".
However, I can still add this to my code:
using System;
using System.IO;
I was just curious as to how come I do not need to have System.dll as a reference in my project to do this. Thanks!
mscorlib.dll includes items in both those namespaces.
You need to right-click your project > Properties > Build > Advanced... and check "Do not reference mscorlib.dll" to remove this reference.
Different assemblies can contribute to the same namespace.
Even if you don't reference System.dll, you are still referencing (implicitly) mscorlib.dll which contributes many types to the System namespace.
These references are probably defined in your Web.config or the Machine.config file so they're included by default.
These are the default libraries.I think your question is that "Dont use third party dlls"
Another thing to consider is, if you're compiling directly through the command line, a default set of switches, including default library references, is parsed by the compiler through the default response file (csc.rsp), located in the same directory as the compiler. The fact that you are able to import namespaces from the Base Class Library without explicitly referencing them at compile time is due to the fact that their containing assemblies are included in your program by default. To change this behavior at the command line, you can use the /nostdlib switch to force it not to include mscorlib.dll, or you can use /noconfig to have it ignore the entire default response file altogether. Also, I'm not too sure what you mean by system.dll, because the namespaces you mentioned are contained within mscorlib.dll. Also, I think by "external library", your instructor must have meant any 3rd party assemblies that would assist you in solving the problem. Anything that comes included with the .NET SDK would be more of a framework library. Unless your teacher is really harsh and wants you to reinvent the wheel :P
If you have
using XXXX.YYYY;
at the top of a C# file, do you need to include that assembly in the References part of the project?
What is the difference?
The references are needed to be added, so that they may be physically located by the compiler at compile time.
For more details watch it at http://en.csharp-online.net/CSharp_FAQ:_Why_add_a_using_statement_and_a_reference
Hope this helps.
Thanks,
Madhup
The "using" keyword is a way of avoiding having to type out the whole namespace for a class every time if it lives outside the current namespace.
For example, if I have namespace foo and I want to reference MyClass in namespace bar I can either write:
bar.MyClass = new bar.MyClass();
or
using bar;
...
MyClass = new MyClass();
The references part of the project tells the compiler which libraries outside the current project to search for the class bar.MyClass
So in short you don't need to put the using statement (but it generally makes the code easier to read and less for you to type) but you do need the referenced assembly.
You don't write using XXXX.dll at the top of a CS File.
I believe you're referring to using NamespaceX; which is a way of categorizing your classes into distinct logical partitions. So I'd group all of my Data Access classes into a namespace called MyProject.DataAccess. An assembly can contain classes belonging to multiple namespaces.
In which case, you need to reference the assembly X if you want to use some types/classes defined in assembly X with that namespace.
The using statement states you want to import a namespace into the file, giving you shorthand access. For example you can write File.Delete(file) instead of System.Io.File.Delete(file) if you imported the System.Io namespace. The namespace you are including should be available in one of your references assemblies. As fasr as I know, you can't reference DLL's directly like that from your code.