Repository navigation
IT CSharp Operators Partial
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Un tipo nativo può eseguire l'overload degli operatori consentiti, dichiarare conversioni e specificare confronti. Tale operazione deve avere un significato chiaro. partial combina le dichiarazioni di tipo in un'unica compilazione; una parte non viene eseguita automaticamente come progetto autonomo.
Salvare questo esempio separatamente come Example.cs. Eseguirlo indipendentemente dagli altri esempi di questa pagina.
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);
}Risultato:
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());Documentazione ufficiale: Sovraccarico dell'operatore e tipi parziali - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE