Skip to main content

How to Resolve Error "CS0562: The parameter of a unary operator must be the containing type" in C#

The Compiler Error CS0562 is a signature restriction error regarding Operator Overloading. The message reads: "The parameter of a unary operator must be the containing type".

In C#, a Unary Operator acts on a single operand (e.g., -x, !x, ~x). If you define such an operator inside a class named MyType, the compiler strictly requires that the operand (the parameter passed to the method) must be of type MyType. You cannot use a custom class to redefine how unary operators work for built-in types like int or string.

This guide explains the correct signature for unary operator overloads.

Understanding Unary Operator Rules

A unary operator takes one input and produces one output.

  • Usage: var result = -obj;
  • Definition: public static ResultType operator -(OperandType arg)

Because this definition lives inside the class OperandType, C# mandates that arg must be that class. Defining operator -(int i) inside class ComplexNumber would imply you are trying to change how -5 behaves globally, which is forbidden.

Affected Operators: + (unary), - (unary), !, ~, true, false.

Scenario: Incorrect Parameter Type

This error often happens when a developer copies a binary operator (like x - y) and tries to convert it into a unary operator (like -x), but forgets to update the parameter list to reflect the class type.

Example of error: attempting to define a logical NOT (!) operator that accepts a boolean instead of the custom class.

public class ToggleSwitch
{
public bool IsOn { get; set; }

// ⛔️ Error CS0562: The parameter of a unary operator must be the containing type.
// This looks like it tries to redefine '!' for the built-in 'bool' type.
public static bool operator !(bool b)
{
return !b;
}
}

Solution: Match the Parameter to the Class

The parameter of the operator must be the class or struct in which the operator is defined.

Solution: change the parameter type to ToggleSwitch.

public class ToggleSwitch
{
public bool IsOn { get; set; }

// ✅ Correct: The operator defines how '!' acts on a 'ToggleSwitch'.
// Signature: operator !(ContainingType x)
public static bool operator !(ToggleSwitch t)
{
return !t.IsOn;
}
}

public class Program
{
static void Main()
{
var ts = new ToggleSwitch { IsOn = true };

// Now valid
if (!ts)
{
System.Console.WriteLine("It is off.");
}
}
}
note

Binary Operators are different: For binary operators (two parameters, like + or *), only one of the parameters needs to be the containing type. This allows MyType + int. Unary operators do not offer this flexibility because there is only one parameter available.

Conclusion

CS0562 ensures that your operators act on your objects.

  1. Identify the Operator: Is it a single-argument operator like !, ~, -, or +?
  2. Check the Parameter: Does the method signature look like operator -(MyClass x)?
  3. The Fix: Ensure the parameter type matches the class name exactly.