C# Frequently Asked Questions

Why did I receive the error: "The type or namespace '<namespace name>' does not exist in the class or namespace '<parent namespace>' (are you missing an assembly reference?)"

Published 29 April 04 09:09 AM

You need to add a reference in your project to an assembly where that namespace is defined.

If you are using VS.NET:

1.  Right click on the References folder on your project.
2.  Select Add Reference.
3.  Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
4.  Double-click the assembly containing the namespace in the error message.
5.  Press the OK button.

If you are using the command line, use the /r: or /reference: option.  For Example:

csc.exe /reference:System.Drawing.dll MyFontDisplayApp.cs

When you recompile, this error will no longer appear.

You can find the assembly, where a namespace is defined, in the documentation.  Identify one of the types in the namespace that you want to use.  Every type in the .NET Framework has an "About" page that provides an overview, basic information about the type, and example code if you're lucky.  At the bottom of the Overview, for all types, there is a "Requirements" section.  This has a Namespace member that tells what namespace the type belongs to.  It also tells what assembly type belongs to, which is the assembly you need to reference in your application.  For example, if I had an application that was using Font types, I would look up Font in the .NET Framework documentation, using the Index tab, and observe that the Font type is in the System.Drawing namespace and its assembly is System.Drawing.dll.

Another question related to this is "If I've already declared a using statement, why do I have to add the reference to the project?"

The reason derives from the fact that the using statement and assembly references have two different purposes.  Recall that the purpose of namespaces is to disambiguate type references and provide logical organization of types.  When specifying a namespace in a using statement, you are telling C# that you want to use the types in that namespace in an unqualified manner.  The key point is that namespaces are "logical" entities that could exist in one or more assemblies.  On the other hand, assemblies are "physical" entities that contain multiple types.  Assemblies are many things, but for the purposes of this discussion, an assembly is the unit of deployment in .NET.  So, the reason you need a reference to the assembly that the type is located in is so that C# can find the physical location of that type, which is not possible with the logical namespace provided by a using statement.

[Author: Joe Mayo ]

Filed under: C# Language and Compiler , IDE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值