When you compile your code you receive the following error message:
The type or namespace name <type/namespace> could not be found (are you missing a using directive or an assembly reference?)
where <type/namespace> is the name of the type or namespace you are trying to use.
There are several reasons why you might be receiving this error:
- You might have misspelled the name of the type or namespace that you are trying to use. Without the correct name, the compiler is unable to find the definition for the type or namespace that you referred to in your code. This occurs most often because C# is case-sensitive and you did not use the correct capitalization when you referred to the type. For example, see the following code:
This will generate compiler error CS0246. Notice the 's' in Dataset is not capitalized.
- If the error is a reference to a namespace, you may not have the assembly where the namespace is located referenced in your project. For example, you might be using the following namespace:
However, if you don't have the assembly Accessibility.dll referenced in your project then you will receive compiler error CS0246.
- If the error is a reference to a type, then you may not have the proper using directive, or you may have not fully qualified the name of the type. See the following line of code:
To be able to use the DataSet type you need two things. First, you need a reference to the assembly that contains the definition for the DataSet type. Second, you need a using directive for the namespace where DataSet is located. For example, because DataSet is located in the System.Data namespace, you would need the following statement at the beginning of your code file:
The second step is not required. However, if you omitted this step, then you would have to fully qualify the DataSet type when you refer to it. Fully qualifying it means that you use the namespace AND type each time that you refer to it in your code. So, if you decided to forgo the second step, you would have to change your declaration code to:
When you receive this compiler error, the first thing to check is spelling. As previously noted, the most common cause is not having the correct case for the type or namespace. If you feel certain that you have spelled the name of the type or namespace correctly (including the correct capitalization), then check the following:
- Assembly Reference:
Make sure that you have referenced the assembly that contains the namespace. If you are developing in Visual Studio .NET, you can go to the Project menu and click Add Reference. On the .NET tab, choose the assembly that contains the namespace that you are trying to import, or Browse to the folder that contains the assembly. If you are using the command-line compiler, add the appropriate switch (/reference) to the compile statement.
Make sure that you are importing the namespace into your project by having a using statement at the top of the code file that contains the type reference, such as:
If you don't have the using statement, then fully qualify the reference to the type:
This behavior is by design.
Article ID: 304656 - Last Review: October 30, 2002 - Revision: 1.0
- Microsoft Visual C# .NET 2002 Standard Edition
|kbprb kbcompiler kbprod2web KB304656|