Skip to content

DE CSharp Operators Partial

Codex edited this page Oct 11, 2026 · 2 revisions

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

Operatorüberladung und Teiltypen

Handbuch C# · Beispiele UO

Ein nativer Typ kann zulässige Operatoren überladen, Konvertierungen deklarieren und Vergleiche angeben. Eine solche Operation muss eine klare Bedeutung haben. partial kombiniert Typdeklarationen in einer Kompilierung; ein Teil wird nicht automatisch als eigenständiges Projekt ausgeführt.

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
var sum = new Count(2) + new Count(3);
System.Console.WriteLine(sum.Value);
readonly record struct Count(int Value)
{
    public static Count operator +(Count a, Count b) => new(a.Value + b.Value);
}

Ergebnis:

5
readonly record struct Amount(int Value)
{
    public static Amount operator +(Amount left, Amount right) => new(left.Value + right.Value);
}
Amount total = new Amount(2) + new Amount(3);
UO.Print(total.Value.ToString());
partial class Counter
{
    public int Value { get; private set; }
}
partial class Counter
{
    public void Increment() => Value++;
}
var counter = new Counter();
counter.Increment();
UO.Print(counter.Value.ToString());

Offizielle Dokumentation: Operatorüberladung und Teiltypen – Microsoft Learn.

Clone this wiki locally