C# Error CS0104 – ‘reference’ is an ambiguous reference between ‘identifier’ and ‘identifier’

C# Compiler Error

CS0104 – ‘reference’ is an ambiguous reference between ‘identifier’ and ‘identifier’

Reason for the Error

You will receive this error when you are trying to access a class where the same name is available in multiple namespaces that are imported in to your class.

For example, let’s have a look at the below code snippet.

using NameSpace1;
using NameSpace2;
namespace NameSpace1
{
    public class Class1
    {
    }
}

namespace NameSpace2
{
    public class Class1
    {
    }
}

public class DeveloperPublish
{
    public static void Main()
    {
        Class1 obj = new Class1();

    }
}

In this program, we have an instance of Class1 created in the class “DeveloperPublish” and Class1 is present in both NameSpace1 and NameSpace2.

You will receive the below error when you compile the above code.

Error CS0104 ‘Class1’ is an ambiguous reference between ‘NameSpace1.Class1’ and ‘NameSpace2.Class1’ ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 21 Active

C# Error CS0104 – 'reference' is an ambiguous reference between 'identifier' and 'identifier'

Solution

To fix the error, use the full name of the class along with the namespace to avoid the conflict.

namespace NameSpace1
{
    public class Class1
    {
    }
}

namespace NameSpace2
{
    public class Class1
    {
    }
}

public class DeveloperPublish
{
    public static void Main()
    {
        NameSpace1.Class1 obj = new NameSpace1.Class1();

    }
}

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...