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

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