Skip to content

DE CSharp Directive Nullable

Codex edited this page Oct 11, 2026 · 2 revisions

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

Nullable-Kontext

Handbuch C# · Praktischer Leitfaden

Verwaltet nullfähige Anmerkungen und nullbare Warnungen. Fügt zur Laufzeit keine automatischen null-Prüfungen ein.

Einfaches Beispiel (Example.cs)

Speichern Sie dieses Beispiel separat als Example.cs. Führen Sie es unabhängig von den anderen Beispielen dieser Seite aus.

Example.cs:

// PROJECT-EXAMPLE
#nullable enable
string? name = null;
System.Console.WriteLine(name ?? "Unknown");

Ergebnis:

Unknown

Syntax

#nullable enable / disable / restore / enable warnings / enable annotations

Beispiel 1

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        string? name = null;
        Console.WriteLine(name ?? "Unknown");
    }
}

Beispiel 2

#nullable enable annotations
#nullable enable warnings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        string? text = "ore";
        if (text is not null) Console.WriteLine(text.Length);
    }
}

Offizielle Quelle: Microsoft Learn.

Clone this wiki locally