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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...