Skip to content

How to Resolve Error "CS0559: The parameter type for ++ or -- operator must be the containing type" in C#

Fix the C# compiler error CS0559 by ensuring your ++ or -- operator accepts the same class type it's defined in, not an int or other type.

The Compiler Error CS0559 is a signature restriction error regarding Operator Overloading. The message reads: "The parameter type for ++ or -- operator must be the containing type".

In C#, the increment (++) and decrement (--) operators are unary operators that modify the state of an operand. The language specification strictly requires that if you define ++ for a class named Counter, the operator must accept a Counter object as input. You cannot define operator ++ inside Counter that takes an int or a different class as its argument.

This guide explains the correct signature for these specific unary operators.

Understanding Increment/Decrement Semantics

When you write x++, the compiler translates this into x = operator ++(x). Because this operation is inherently about "incrementing x", the input to the operator logic must be the object x itself.

If you are inside class MyType, the signature must be: public static MyType operator ++(MyType x)

Defining operator ++(int i) inside MyType makes no sense logically: what would 5++ mean inside the context of your custom class?

Scenario: Incorrect Parameter Type

This error often occurs when a developer confuses the + operator (which can take different types) with the ++ operator.

Example of error: attempting to define an increment operator that takes an integer, perhaps thinking this allows obj ++ 5.

public class Counter
{
    public int Value { get; set; }

    // ⛔️ Error CS0559: The parameter type for ++ or -- operator 
    // must be the containing type ('Counter').
    // You cannot pass an 'int' to the ++ operator of 'Counter'.
    public static Counter operator ++(int i)
    {
        return new Counter { Value = i + 1 };
    }
}

Solution: Match the Containing Type

The operator must accept the class it is defined in.

Solution: change the parameter type to Counter.

public class Counter
{
    public int Value { get; set; }

    // ✅ Correct: Takes 'Counter' and returns 'Counter'.
    public static Counter operator ++(Counter c)
    {
        // Note: It is good practice to return a NEW object rather than modifying the input,
        // though C# handles the reassignment automatically.
        return new Counter { Value = c.Value + 1 };
    }
    
    // Same rule applies to --
    public static Counter operator --(Counter c)
    {
        return new Counter { Value = c.Value - 1 };
    }
}

public class Program
{
    static void Main()
    {
        var c = new Counter { Value = 10 };
        c++; // Calls operator ++(c)
        System.Console.WriteLine(c.Value); // 11
    }
}

Conclusion

CS0559 enforces the logic of self-modification.

  1. Check the Operator: Is it ++ or --?
  2. Check the Class: What class is it inside? (e.g., MyClass).
  3. Check the Parameter: The operator must look like public static MyClass operator ++(MyClass arg).
Navigation