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?)"

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?)"

  • Comments 43

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]

Leave a Comment
  • Please add 1 and 4 and type the answer here:
  • Post
  • excellent - short and to the point. thanks
  • I'm tring to use this method (StrToInt), but when i try, this error apears "The type or namespace does not exist in the class or namespace", what should i do?
  • I'm tring to use this method (StrToInt), but when i try, this error apears "The type or namespace does not exist in the class or namespace", what should i do?
  • thanks....helped me.
  • I'm using assembly references for shared code in a large project (100+ assemblies). We routinely get this same error--despite having references to the specified assembly--when (it seems) different "major" assemblies have been compiled with references to different versions of the same utility assembly. The signatures of the utility assemblies has not changed.

    This is HUGELY frustrating--and has a large development team grumbling.
  • Thanks so much!!! I searched several places, including VS help, and do you think anybody would tell me to right click on reference??

    Not until I found this site. Thank you !!!!
  • still i couldn't use methods in the referenced project. I used class library project. Please help me. I got stuck in that.

  • This error can also appear even when you have an appropriate reference & using statement.  Generally it happens when the compiled version is out of date and the code you're working on has a number of syntax errors (maybe your mid conversion/refactor of a block of code).

    In this situation you can resolve the error by choosing "Clean Solution" or "Clean <Your Poject Name>" from the Build menu in Visual Studio.

  • Thanks EricB, you resolved my problem.

  • You should also check that the "Build Action" of the .cs file is set to 'compile'

  • Thank you, I have looked for it so much

  • Damn, is it so hard to give a clear answer to something?

    apparently not! Thank you very much!

  • I have a Windows Project called Surveys and a Class Library project called Surveys.Library and I can clearly see the reference to Surveys.Library sitting within Surveys but am still getting this error. I cannot see why this would be. Every time I delete the reference and re-add it, it's fine until I hit f5 and it doesn't pick up the reference even though it's still there.

  • Okay, answered my own question. A bad using reference in the library seems to break the reference in the Main project but the system never attributes the bad library reference as being the problem.

Page 1 of 3 (43 items) 123