How to Resolve Error "CS0075: To cast a negative value, you must enclose the value in parentheses" in C#
The Compiler Error CS0075 is a syntax parsing error. The message reads: "To cast a negative value, you must enclose the value in parentheses."
This error occurs due to the order of operations (operator precedence) in C#. When you cast a negative literal to a type that is not a built-in keyword (e.g., casting to an Enum or a struct like Int32 instead of int), the parser gets confused. It struggles to determine if the minus sign - belongs to the number or if it represents a subtraction operation happening after the cast.
This guide explains why this ambiguity happens and how to fix the syntax.
Understanding the Parsing Issue
In C#, a Cast (Type) has very high operator precedence.
When you write:
(MyType) -5
If MyType is a standard C# keyword (like int, double, float), the compiler has special rules to handle this gracefully.
However, if MyType is a user-defined type (like an Enum) or a .NET Framework type name (like Int32), the parser interprets (MyType) as the cast operation and then sees the - operator. It is unsure if you meant "Cast the number -5" or "Cast something else, then subtract 5". To resolve this ambiguity, the compiler enforces strict syntax: you must bind the negative sign to the number using parentheses.
Scenario 1: Casting to Enums and Non-Keyword Types
This error is most frequently encountered when working with Enums that have negative values, or when using the formal .NET type names (like System.Float) instead of C# aliases (like float).
Example: attempting to cast a negative literal directly to an Enum or a specific struct type.
public enum Status
{
Error = -1,
Success = 0
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0075: To cast a negative value, you must enclose the value in parentheses
// The compiler is confused by the space or relationship between (Status) and -1.
Status s = (Status) -1;
// ⛔️ Error CS0075: Also happens with .NET struct names
// 'Double' is a class/struct name, 'double' is a keyword.
var val = (System.Double) - 5.5;
}
}
Why does (int) -5 work?
The C# compiler treats keywords like int, long, and double specially. (int) -5 is unambiguous to the parser. The error only triggers for types that are not native keywords.
Solution: Add Parentheses
To fix this, you simply need to wrap the negative number in parentheses. This tells the compiler explicitly: "The minus sign belongs to the number 5, creating a negative literal. Please cast that resulting negative number."
The Correct Syntax:
public enum Status
{
Error = -1,
Success = 0
}
public class Program
{
static void Main()
{
// ✅ Correct: Enclose the negative value in parentheses
Status s = (Status)(-1);
// ✅ Correct: Enclose the negative float/double
var val = (System.Double)(-5.5);
System.Console.WriteLine($"Status: {s}");
}
}
Output:
Status: Error
Conclusion
CS0075 is a syntax enforcement rule to prevent ambiguity in mathematical operations.
- Identify the Cast: Are you casting to a custom type, an Enum, or a system type like
Int32? - Identify the Value: Is it a negative literal (e.g.,
-1,-99)? - Group the Value: Change
(Type) -Valueto(Type)(-Value).