C# Error CS0012 – The type ‘type’ is defined in an assembly that is not referenced

C# Compiler Error Message

Error CS0012 The type ‘type is defined in an assembly that is not referenced. You must add a reference to assembly.

Reason for the Error

You will see this error especially when the referenced type was not found. In simple terms, if the assembly (dll) file is not included in the compilation, you will see this error.

For example, Assume that you have a Class Library “ClassLibrary1” which has the below class defined.

namespace ClassLibrary1
{
    public class ClassA {
    }
}

You then have another Class Library “ClassLIbrary2” which references ClassLibrary1 and has the following class definition.

using ClassLibrary1;

namespace ClassLibrary2
{
    public class ClassB
    {
        public static ClassA Method1()
        {
            return new ClassA();
        }
    }
}

You have a ConsoleApp1 with the below class that references ClassLibrary2 and calls Method1 of ClassB as shown below.

using ClassLibrary2;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            object o = ClassB.Method1();
        }
    }
}

When you compile the ConsoleApp1, you will see the below error.

Error CS0012 The type ‘ClassA’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’

C# Error CS0012 - The type 'type' is defined in an assembly that is not referenced

Solution

To fix the C# compiler error CS0012, just ensure that you add the reference to both the ClassLibraries (ClassLibrary1,ClassLibrary2) in Visual Studio by using “Add Reference Dialog”.

C# Error CS0012 - The type 'type' is defined in an assembly that is not referenced

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...