# CS0019 — Operator cannot be applied to operands
## Pattern
`error CS0019: Operator 'X' cannot be applied to operands of type 'T1' and 'T2'`
## Root Causes (ordered by likelihood)
1. Comparing a `bool` to a `double` or numeric type (e.g., `if (someCondition == 1.0)`)
2. Using `==` or `!=` on a nullable type (`double?`, `int?`) without unwrapping `.Value`
3. Arithmetic between incompatible types without an explicit cast (e.g., `int` and `double` in a context where implicit conversion is blocked)
4. Comparing an enum value to an integer literal without a cast
## Fix Strategies
### Strategy 1: Bool-to-numeric comparison
- **Detect**: The error message shows operand types like `bool` and `double`, `bool` and `int`, or similar. The code is comparing a boolean expression directly to a number.
- **Lookup**: In C#, `bool` cannot be compared to numeric types. The comparison must be rewritten as a boolean expression.
- **Fix**: Remove the numeric comparison and use the boolean directly, or compare the numeric value to the threshold separately.
- **Verify**: Both operands of the operator are the same type (or have a defined operator overload).
```csharp
// Wrong: comparing bool to double
bool isAbove = Close[0] > High[1];
if (isAbove == 1.0) { ... } // CS0019
// Correct:
if (isAbove) { ... }
// or
if (Close[0] > High[1]) { ... }
```
### Strategy 2: Nullable type without `.Value`
- **Detect**: One operand type in the error is `T?` (e.g., `double?`, `int?`). The operator is `+`, `-`, `*`, `/`, `<`, `>`, `==`, or similar.
- **Lookup**: Nullable types in C# don't support most operators directly. You must either unwrap with `.Value` (after a null check) or use the null-coalescing operator `??` to provide a fallback.
- **Fix**: Add a null check and use `.Value`, or use `?? defaultValue` to unwrap safely.
- **Verify**: The nullable is unwrapped before the operator is applied, and a null guard prevents a `NullReferenceException` at runtime.
```csharp
// Wrong: nullable double in arithmetic
double? maybePrice = GetOptionalPrice();
double result = maybePrice + 10.0; // CS0019
// Correct option A: null check + .Value
if (maybePrice.HasValue)
double result = maybePrice.Value + 10.0;
// Correct option B: null-coalescing
double result = (maybePrice ?? 0.0) + 10.0;
```
### Strategy 3: Enum compared to integer literal
- **Detect**: One operand is an enum type (e.g., `MarketPosition`, `State`, a custom DEEP6 enum) and the other is an `int` literal.
- **Lookup**: C# enums are not implicitly convertible to `int` in comparisons. Use the enum member name directly, or cast the integer to the enum type.
- **Fix**: Replace the integer literal with the named enum member, or cast: `(MyEnum)intValue`.
- **Verify**: Both sides of the comparison are the same enum type.
```csharp
// Wrong: comparing MarketPosition to int
if (Position.MarketPosition == 1) { ... } // CS0019
// Correct:
if (Position.MarketPosition == MarketPosition.Long) { ... }
```
### Strategy 4: Implicit cast blocked between numeric types
- **Detect**: The error shows operand types like `int` and `double`, or `float` and `decimal`, in a context where the compiler cannot apply an implicit conversion (e.g., inside a generic or when the result type is ambiguous).
- **Lookup**: Identify which operand needs to be cast. In NT8, `Close[0]` returns `double`; `CurrentBar` returns `int`. Mixing them in arithmetic usually works via implicit widening, but explicit casts are needed when the target type is narrower.
- **Fix**: Add an explicit cast to align the types: `(double)intValue` or `(int)doubleValue` (with awareness of truncation).
- **Verify**: The cast produces the intended type and the operator is valid for that type.
```csharp
// Wrong: int result from double division assigned to int without cast
int bars = CurrentBar / 2.5; // CS0019 or CS0266
// Correct:
int bars = (int)(CurrentBar / 2.5);
```
## NT8-Specific Type Notes
| NT8 Expression | C# Type | Common Mistake |
|----------------|---------|----------------|
| `Close[0]` | `double` | Compared to `int` literal |
| `CurrentBar` | `int` | Used in `double` arithmetic without cast |
| `Position.MarketPosition` | `MarketPosition` (enum) | Compared to `int` |
| `State` | `State` (enum) | Compared to `int` |
| `Volume[0]` | `double` | Compared to `long` |
| `IsFirstTickOfBar` | `bool` | Compared to `1` or `0` |
## Example Fix
```diff
// Checking if we're in a long position
- if (Position.MarketPosition == 1) // CS0019: MarketPosition vs int
+ if (Position.MarketPosition == MarketPosition.Long)
{
// ...
}
// Nullable price check
double? optPrice = GetLevel();
- double spread = optPrice - Close[0]; // CS0019: double? vs double
+ double spread = (optPrice ?? Close[0]) - Close[0];
```