Files
nt8-sdk/.kilocode/rules/csharp_50_syntax.md
2026-02-24 15:00:41 -05:00

2.6 KiB

C# 5.0 Syntax — Required for NT8 SDK

This project targets .NET Framework 4.8 and must use C# 5.0 syntax only.
NinjaTrader 8's NinjaScript compiler does not support C# 6+ features.


Forbidden Patterns (with fixes)

String Interpolation (C# 6)

// ❌ NEVER
_logger.LogInformation($"Order {orderId} filled at {price}");

// ✅ ALWAYS
_logger.LogInformation("Order {0} filled at {1}", orderId, price);

Null-Conditional Operator (C# 6)

// ❌ NEVER
var name = order?.Symbol;

// ✅ ALWAYS
var name = order != null ? order.Symbol : null;

Null-Coalescing Assignment (C# 8)

// ❌ NEVER
value ??= defaultValue;

// ✅ ALWAYS
if (value == null) value = defaultValue;

Expression-Bodied Members (C# 6)

// ❌ NEVER
public int Contracts => _contracts;
public void Reset() => _contracts = 0;

// ✅ ALWAYS
public int Contracts { get { return _contracts; } }
public void Reset() { _contracts = 0; }

nameof Operator (C# 6)

// ❌ NEVER
throw new ArgumentNullException(nameof(intent));

// ✅ ALWAYS
throw new ArgumentNullException("intent");

Auto-Property Initializers (C# 6)

// ❌ NEVER
public bool IsEnabled { get; set; } = true;

// ✅ ALWAYS — initialize in constructor
public bool IsEnabled { get; set; }
public MyClass() { IsEnabled = true; }

Inline Out Variable Declaration (C# 7)

// ❌ NEVER
if (_orders.TryGetValue(id, out var status)) { ... }

// ✅ ALWAYS
OrderStatus status;
if (_orders.TryGetValue(id, out status)) { ... }

Pattern Matching (C# 7)

// ❌ NEVER
if (obj is string s) { ... }

// ✅ ALWAYS
if (obj is string) { var s = (string)obj; ... }

Local Functions (C# 7)

// ❌ NEVER — function inside a method
public void Execute() {
    void Helper() { ... }
    Helper();
}

// ✅ ALWAYS — use private methods
private void Helper() { ... }
public void Execute() { Helper(); }

Tuple Literals (C# 7)

// ❌ NEVER
var result = (price: 100.0, qty: 5);

// ✅ ALWAYS — use Tuple<T1,T2> or a named class
var result = Tuple.Create(100.0, 5);

using static (C# 6)

// ❌ NEVER
using static System.Math;

// ✅ ALWAYS
System.Math.Floor(x);
Math.Floor(x); // (via standard using System;)

Quick Self-Check Before Saving

  • Search your code for $" — if found, replace every occurrence
  • Search for ?. — if found, replace with null check
  • Search for => — if on a property or method, rewrite as full block
  • Search for nameof — replace with string literal
  • Search for out var — split into declaration + assignment