Skip to content

How to Resolve Error "CS0023: Operator 'operator' cannot be applied to operand of type 'type'" in C#

Fix the C# compiler error CS0023 when applying unary operators like !, -, or ~ to incompatible types (e.g., strings, objects, or custom classes) with proper boolean checks, numeric validation, or operator overloading.

The Compiler Error CS0023 is a Unary Operator Mismatch. For example, the message states: "Operator '!' cannot be applied to operand of type 'string'" (or similar operators like -, ~, +).

This error occurs when you apply an operator that works on a single variable (a unary operator) to a data type that does not support it. This is most common when developers coming from languages like C++, JavaScript, or Python try to use "truthy/falsy" logic (e.g., if (!myObject)) which is not valid in C#.

This guide explains which operators trigger this and how to write the correct C# equivalents.

Understanding Unary Operators

A Unary Operator acts on a single operand.

  • !a (Logical NOT)
  • -a (Numeric Negation)
  • ~a (Bitwise Complement)
  • ++a (Increment)

CS0023 is distinct from CS0019 (which involves two operands, like a + b). CS0023 specifically means the variable you are targeting doesn't have a definition for that specific single-action operator.

Scenario 1: The Logical NOT (!) on Non-Booleans

In JavaScript or C++, it is common to check if a list has items using if (!list.count) or check if a string is empty using if (!text).

In C#, the ! operator is strictly for booleans. You cannot apply it to integers, strings, or null objects directly.

Example of Mistake

public void CheckData()
{
    string name = "Alice";
    int count = 0;

    // ⛔️ Error CS0023: Operator '!' cannot be applied to operand of type 'string'
    if (!name) 
    {
        // ...
    }

    // ⛔️ Error CS0023: Operator '!' cannot be applied to operand of type 'int'
    if (!count)
    {
        // ...
    }
}

Solution

You must write an explicit boolean comparison.

public void CheckData()
{
    string name = "Alice";
    int count = 0;

    // ✅ Correct: Explicitly check for null or empty
    if (string.IsNullOrEmpty(name))
    {
        System.Console.WriteLine("Name is missing");
    }

    // ✅ Correct: Explicitly compare the integer
    if (count == 0)
    {
        System.Console.WriteLine("Count is zero");
    }
}

Scenario 2: Mathematical Negation (-) on Incompatible Types

You can put a minus sign in front of an int, double, or decimal to invert its sign. You cannot put a minus sign in front of a string or a generic object.

Example of Mistake

public void ProcessText()
{
    string text = "Hello";

    // ⛔️ Error CS0023: Operator '-' cannot be applied to operand of type 'string'
    // C# does not support "negative strings".
    var inverse = -text;
}

Solution

If you intended to reverse the string, you must use string manipulation methods.

using System.Linq;

public void ProcessText()
{
    string text = "Hello";

    // ✅ Correct: Using LINQ to reverse a string
    string reversed = new string(text.Reverse().ToArray());
    // Output: "olleH"
}

Scenario 3: Operator Overloading for Custom Types

If you create a custom class (like a vector, a complex number, or a monetary value) and try to use unary operators on it, the compiler will throw CS0023 unless you explicitly define those operators.

Example of Mistake

public class ComplexNumber
{
    public int Real { get; set; }
    public int Imaginary { get; set; }
}

public class Program
{
    static void Main()
    {
        var num = new ComplexNumber { Real = 1, Imaginary = 2 };

        // ⛔️ Error CS0023: Operator '-' cannot be applied to operand of type 'ComplexNumber'
        var negativeNum = -num;
    }
}

Solution

Define a static operator overload inside your class.

public class ComplexNumber
{
    public int Real { get; set; }
    public int Imaginary { get; set; }

    // ✅ Correct: Teach the compiler what '-' means for this class
    public static ComplexNumber operator -(ComplexNumber c)
    {
        return new ComplexNumber 
        { 
            Real = -c.Real, 
            Imaginary = -c.Imaginary 
        };
    }
}

public class Program
{
    static void Main()
    {
        var num = new ComplexNumber { Real = 1, Imaginary = 2 };
        
        // Now this works
        var negativeNum = -num;
    }
}

Conclusion

CS0023 enforces type safety for unary operations.

  1. Check !var: Ensure var is actually a boolean. If it's a string, number, or object, use explicit comparisons (!= null, == 0, string.IsNullOrEmpty).
  2. Check -var or ~var: Ensure the variable is a numeric type.
  3. Custom Classes: If you want to use these symbols on your own objects, you must implement Operator Overloading.
Navigation