C# Error CS1614 – ‘{0}’ is ambiguous between ‘{1}’ and ‘{2}’. Either use ‘@{0}’ or explicitly include the ‘Attribute’ suffix.

C# Error

CS1614 – ‘{0}’ is ambiguous between ‘{1}’ and ‘{2}’. Either use ‘@{0}’ or explicitly include the ‘Attribute’ suffix.

Reason for the Error & Solution

‘name’ is ambiguous between ‘name’ and ‘nameAttribute’; use either ‘@name’ or ‘nameAttribute’.

The compiler has encountered an ambiguous attribute specification.

For convenience, the C# compiler allows you to specify ExampleAttribute as just [Example]. However, ambiguity arises if an attribute class named Example exists along with ExampleAttribute, because the compiler cannot tell if [Example] refers to the Example attribute or the ExampleAttribute attribute. To clarify, use [@Example] for the Example attribute and [ExampleAttribute] for ExampleAttribute.

The following sample generates CS1614:

// CS1614.cs  
using System;  
  
// Both of the following classes are valid attributes with valid  
// names (MySpecial and MySpecialAttribute). However, because the lookup  
// rules for attributes involves auto-appending the 'Attribute' suffix  
// to the identifier, these two attributes become ambiguous; that is,  
// if you specify MySpecial, the compiler can't tell if you want  
// MySpecial or MySpecialAttribute.  
  
public class MySpecial : Attribute {  
   public MySpecial() {}  
}  
  
public class MySpecialAttribute : Attribute {  
   public MySpecialAttribute() {}  
}  
  
class MakeAWarning {  
   [MySpecial()] // CS1614  
                 // Ambiguous: MySpecial or MySpecialAttribute?  
   public static void Main() {  
   }  
  
   [@MySpecial()] // This isn't ambiguous, it binds to the first attribute above.  
   public static void NoWarning() {  
   }  
  
   [MySpecialAttribute()] // This isn't ambiguous, it binds to the second attribute above.  
   public static void NoWarning2() {  
   }  
  
   [@MySpecialAttribute()] // This is also legal.  
   public static void NoWarning3() {  
   }  
}  

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