Skip to content

How to Resolve Error "CS0590: User-defined operators cannot return void" in C#

Fix the C# compiler error CS0590 by ensuring operator overloads return a value (e.g., the class type for arithmetic operators or bool for comparisons) instead of void.

The Compiler Error CS0590 is a syntax restriction error regarding Operator Overloading. The message reads: "User-defined operators cannot return void".

In C#, operators (like +, -, *, ==) are designed to be part of expressions. An expression evaluates to a value. For example, in the statement var result = a + b;, the + operation produces a result that is assigned to result. If an operator returned void (nothing), it would be impossible to use it in assignments, mathematical formulas, or conditional checks. Therefore, C# enforces that all operator overloads must return a value.

This guide explains the correct return types for operator overloading.

Understanding Operator Expressions

When you write code like int x = 5 + 5;, the + operator takes two integers and returns a new integer (10).

If you were allowed to define void operator +, the syntax a + b would become a statement that does work but produces no result. This would break fundamental language constructs:

  • c = a + b; (Invalid: cannot assign void to c).
  • func(a + b); (Invalid: cannot pass void as an argument).

To maintain consistency with built-in types, custom operators must return a result.

Scenario: Defining a Void Operator

This error often occurs when a developer tries to treat an operator like a method that modifies the object in-place (mutation), rather than following the standard pattern of returning a new, modified object (immutability).

Example of error: attempting to make the + operator simply update a field inside the class without returning anything.

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

    // ⛔️ Error CS0590: User-defined operators cannot return void.
    // The compiler expects this to return a 'Score' object.
    public static void operator +(Score a, int points)
    {
        a.Value += points;
    }
}

public class Program
{
    static void Main()
    {
        var s = new Score { Value = 10 };
        
        // If the above were valid, this line would look weird:
        // s + 5; 
        // It looks like an expression but acts like a void method call.
    }
}

Solution: Return a New Object

The standard contract for binary operators is to return a new instance representing the result of the operation, leaving the original operands unchanged.

Solution: change the return type from void to the class type (e.g., Score).

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

    // ✅ Correct: Returns a new 'Score' object.
    public static Score operator +(Score a, int points)
    {
        return new Score 
        { 
            Value = a.Value + points 
        };
    }
}

public class Program
{
    static void Main()
    {
        var s1 = new Score { Value = 10 };
        
        // Now works as a standard expression
        var s2 = s1 + 5; 
        
        System.Console.WriteLine(s2.Value); // 15
    }
}

Conclusion

CS0590 ensures that your custom operators behave like standard C# operators.

  1. Check the Signature: Look at the return type of your operator method.
  2. Change void:
    • For Math (+, -, *): Return the Containing Class (or a numeric type).
    • For Logic (==, !=, <): Return bool.
    • For Conversion (implicit, explicit): Return the Target Type.
Navigation