Skip to content

EN CSharp Directives

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Directives, language version and compilation options

Manual C# · Examples UO

Directives start with # at the beginning of the line: #if/#elif/#else/#endif, #define, #undef, #region, #endregion, #warning, #error, #line, #pragma, #nullable. They control compilation or diagnostics. #region organizes the editor, but does not create a scope.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
#define REPORT
#if REPORT
System.Console.WriteLine("Enabled");
#else
System.Console.WriteLine("Disabled");
#endif

Result:

Enabled
#nullable enable
string? name = null;
UO.Print(name ?? "unknown");
#if DEBUG
UO.Print("Debug configuration");
#else
UO.Print("Regular configuration");
#endif

A typical project defines TargetFramework, LangVersion, Nullable, AllowUnsafeBlocks and other properties in .csproj. The syntax is determined by the compiler, and the available API is also determined by the target platform and dependencies.

Of the modern features of C#, raw strings, collection expressions, primary constructors, list patterns, required, advanced ref capabilities, extensions and properties with field are important. For exact terms of a particular version, use the official history of the language; preview capabilities are enabled by a separate project parameter.

Official documentation: Directives, language version and compilation options - Microsoft Learn.

Clone this wiki locally