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
Copy link
Powered by Social Snap