Skip to content

EN CSharp Operators Partial

Codex edited this page Oct 11, 2026 · 2 revisions

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

Operator overloading and partial types

Manual C# · Examples UO

A user-defined type can overload permitted operators, declare conversions and define comparisons. Give each operation a clear meaning. partial combines type declarations in one compilation; each part is not a separate runnable project.

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
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);
}

Result:

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());

Official documentation: Operator Overloading and Partial Types - Microsoft Learn.

Clone this wiki locally