C# FAQ: Am I missing an assembly reference
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
Contents |
Am I missing an assembly reference?
Most developers encounter the following error frequently:
| Error (program output) |
| The type or namespace '<namespace name>' does not exist in
the class or namespace '<parent namespace>' (are you missing an assembly reference?) |
This error message indicates that a reference must be added in the project to an assembly where that namespace is defined.
Adding a reference using Visual Studio .NET
When using Visual Studio .NET, follow these steps to add a reference:
- Right click on the References folder on your project.
- Select Add Reference.
- Select the .NET tab; or, select the Browse button if not a .NET Framework assembly.
- Double click the assembly containing the namespace named in the error message.
- Press the OK button.
Adding a reference from the Windows command line
When using the command line, use the /r: or /reference: option. Here is an example:
csc.exe /reference:System.Drawing.dll FontDisplayAppl.cs
Upon recompilation, the error message will have magically disappeared!
To find the assembly in which a namespace is defined, check the documentation. Identify a type in the namespace that must be used. Every type in the .NET Framework has an "About" page which gives a brief overview, some basic type information, and—hopefully—sample source code.
Near the bottom of the "Overview" section, find the "Requirements" section. It contains a Namespace member which specifies the namespace to which the type belongs. Also, this section lists to which assembly a type belongs as well as which assembly the application must reference.
For instance, if an application uses Font types, look up Font in the .NET Framework documentation using the Index tab. Note that the Font type is in the System.Drawing namespace and its assembly is System.Drawing.dll.