Repository navigation
ES CSharp Operators Partial
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Un tipo nativo puede sobrecargar operadores permitidos, declarar conversiones y especificar comparaciones. Una operación de este tipo debe tener un significado claro. partial combina declaraciones de tipo en una compilación; una parte no se ejecuta automáticamente como un proyecto independiente.
Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.
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);
}Resultado:
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());Documentación oficial: Sobrecarga de operador y tipos parciales - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE