Skip to main content

How to Resolve Error "CS0035: Operator 'operator' is ambiguous on an operand of type 'type'" in C#

The Compiler Error CS0035 is a specific Ambiguity Error related to Unary Operators (operators that work on a single variable, like -, !, ~, ++, or --). For example, a message could be: "Operator '-' is ambiguous on an operand of type 'MyClass'".

This error occurs when you apply a unary operator to an object, and the compiler finds multiple valid ways to perform that operation via Implicit Conversions, but cannot determine which one is the intended "best" match.

This guide explains why this happens and how to resolve the ambiguity.

Understanding Unary Operator Ambiguity

A Unary Operator acts on one operand (e.g., -x negates x). When you write -x on a custom object, the compiler follows this logic:

  1. Does the class of x define an operator - explicitly?
  2. If not, can x be implicitly converted to a type that does have a - operator (like int, long, float, decimal)?

CS0035 triggers if step 2 yields two or more valid types, and neither type is considered "better" (more specific) than the other according to C# overload resolution rules.

note

CS0034 vs CS0035:

  • CS0034: Ambiguity on Binary operators (e.g., a + b).
  • CS0035: Ambiguity on Unary operators (e.g., -a).

Scenario: Implicit Conversions to Multiple Numeric Types

The most common cause is a class that allows implicit conversion to both integer-based types (like int) and floating-point types (like double or decimal). Both of these types support the unary minus (-) operator.

Example of Ambiguous Code

In this example, SmartNumber can turn into an int or a double automatically.

public class SmartNumber
{
// Allows: int x = new SmartNumber();
public static implicit operator int(SmartNumber n) => 10;

// Allows: double y = new SmartNumber();
public static implicit operator double(SmartNumber n) => 10.5;
}

public class Program
{
static void Main()
{
SmartNumber num = new SmartNumber();

// ⛔️ Error CS0035: Operator '-' is ambiguous on an operand of type 'SmartNumber'
// Path A: Convert to int, then negate (Result: -10)
// Path B: Convert to double, then negate (Result: -10.5)
// The compiler doesn't know which math logic you want.
var negative = -num;
}
}

If the compiler chose int, you would lose precision (data loss). If it chose double, the behavior might change. Therefore, it forces you to choose.

Solution: Explicit Casting

To fix the error, you must tell the compiler exactly which conversion path to take by using an Explicit Cast.

So, cast the variable to the desired type before the operator is applied.

public class Program
{
static void Main()
{
SmartNumber num = new SmartNumber();

// ✅ Correct: Treat it as an integer explicitly
int intResult = -(int)num;
System.Console.WriteLine($"Int Negation: {intResult}");

// ✅ Correct: Treat it as a double explicitly
double doubleResult = -(double)num;
System.Console.WriteLine($"Double Negation: {doubleResult}");
}
}

Output:

Int Negation: -10
Double Negation: -10.5

Conclusion

CS0035 ensures that unary operations are deterministic.

  1. Analyze the Class: Check if your class has multiple implicit operator definitions.
  2. Identify the Conflict: Do both target types support the operator you are using (e.g., - or ~)?
  3. Cast Explicitly: Use -(int)variable or -(double)variable to remove the ambiguity.