C# 12

Last reviewed Sep 5, 2026

Export as PDF

Prompt Details

Prompt Composition

1prompt
  1. C# 12124 lines · 4.2K tokens

CWE Coverage

  • 20
  • 22
  • 59
  • 78
  • 89
  • 94
  • 117
  • 190
  • 208
  • 209
  • 214
  • 257
  • 295
  • 323
  • 327
  • 338
  • 362
  • 367
  • 378
  • 426
  • 502
  • 532
  • 674
  • 759
  • 770
  • 772
  • 775
  • 798
  • 915
  • 916
  • 918
  • 1333
  • 1395

Prompt Preview

Apply these rules whenever you write or review C# 12. Treat nullable annotations, generic constraints, and static types as design controls, not substitutes for validating runtime data at each trust boundary.

Preserve the rules through framework adapters, generated code, serializers, interop layers, and background work.

Compiler and type safety

  • Set <Nullable>enable</Nullable> for the whole project and prevent file-level #nullable disable from hiding warnings at request, configuration, serialization, or identity boundaries.
  • Pin the compiler warning wave with <WarningLevel>; since the .NET 7 SDK an unset value follows <AnalysisLevel>, which defaults to the target framework and also selects the CA analyzer set.
  • Make compiler and CA warnings fail CI with -warnaserror or equivalent MSBuild policy.
  • Avoid the null-forgiving operator on request, configuration, lookup, deserialization, and authentication results.
  • Model validated identifiers, paths, URLs, and security states as distinct types rather than interchangeable strings.
  • Use exhaustive pattern matching for closed security states and reject unknown enum values received at runtime.

Input conversion and validation

  • Validate length and syntax before parsing attacker-controlled text into numbers, dates, identifiers, or structured values.
  • Use culture-invariant parsing with an exact format for protocol, storage, and security-sensitive values.
  • Use TryParse overloads with explicit styles and culture instead of exception-driven or permissive conversion, then check application-specific bounds