Compiler Error CS0246
TOC
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

Compiler Error CS0246

Updated: July 2008

The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)

A type was not found. You may have forgotten to reference (/reference) the assembly that contains the type or you may not have correctly qualified its use with the using directive.

There are several reasons for this error:

  • The name of the type or namespace you are trying to use may be misspelled (including the correct case). Without the correct name the compiler is unable to find the definition for the type or namespace you have referred to in your code. This occurs most often because C# is case-sensitive and the correct casing has not been used when referring to the type. For example, Dataset ds; will generate CS0246; notice the s in Dataset is not capitalized.

  • If the error is for a namespace name, you may not have referenced (/reference) the assembly containing the namespace. For example, your code might contain using Accessibility;. However, if your project doesn't reference the assembly Accessibility.dll then you will get CS0246. See Add Reference Dialog Box for information on how to add a reference in the development environment.

  • If the error is for a type name, you may not have the proper using directive, or you have not fully qualified the name of the type. Consider the following line of code: DataSet ds;. To be able to use the DataSet type you would need to do 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: using System.Data;.

  • The second step is not required. However, if you omitted this step then it would require that you fully qualify the DataSet type when referring to it. Fully qualifying it means that you use the namespace and type each time you refer to it in your code. So, if you decided to skip the second step you would need to change your declaration code above to: System.Data.DataSet ds;.

  • If the error is for a non-type, you may have used a variable or something else when a type was expected. For example, in the is statement, if you use a Type object rather than an actual type, you will get this error.

  • If you use a using alias directive and do not fully qualify the type name, this error is generated. A using alias directive does not use other using directives in the source code file to resolve types. For example, the following code generates CS0246 because the type List<int> is not fully qualified, even though a using directive for System.Collections.Generic is present:

    using System.Collections.Generic;
    // Generates CS0246:
    using myAliasName = List<int>; 
    // Does not generate an error:
    using myAliasName2 = System.Collections.Generic.List<int> 
    

The following sample generates CS0246:

// CS0246.cs
// using System.Diagnostics;

public class MyClass
{
   [Conditional("A")]   // CS0246, uncomment using directive to resolve
   public void Test()
   {
   }

   public static void Main()
   {
   }
}

Here's an example where an object of type Type was used where an actual type was expected (case 4 above):

// CS0246b.cs
using System;

class C
{
    public bool supports(object o, Type t)
    {
         if (o is t)  // CS0246 – t is not a type
         {
            return true;
         }
         return false;
    }

    public static void Main()
    {
    }
}

Date

History

Reason

July 2008

Added possible cause for the error.

Information enhancement.

Show:
© 2016 Microsoft