Repository navigation
KO CSharp Keyword operator
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
사용자 정의 유형에 대해 지원되는 작업 동작을 지정합니다. 매개변수 개수와 정적/인스턴스 형식은 연산자에 따라 다릅니다.
이 예제를 Example.cs로 따로 저장하고 이 페이지의 다른 예제와 독립적으로 실행하세요.
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);
}결과:
5
public static Type operator +(Type left, Type right) => ...;인스턴스 멤버 및 인스턴스 ++/--인 복합 할당 연산자가 C# 14에 추가됩니다. ==/!= 및 해당 비교에는 적절한 쌍별 선언이 필요합니다.
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)
{
Amount total = new Amount(2) + new Amount(3);
Console.WriteLine(total.Value);
}
}
public readonly record struct Amount(int Value) { public static Amount operator +(Amount left, Amount right) => new(left.Value + right.Value); }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)
{
Counter value = new();
value += 5; value++;
Console.WriteLine(value.Value);
}
}
public class Counter { public int Value { get; private set; } public void operator +=(int amount) { Value += amount; } public void operator ++() { Value++; } }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)
{
Flag value = new(true);
if (value) Console.WriteLine("Enabled");
}
}
public readonly record struct Flag(bool Enabled) { public static bool operator true(Flag value) => value.Enabled; public static bool operator false(Flag value) => !value.Enabled; }공식 출처: Microsoft 학습.
ClassicUO — Bad Newbie & C# Scripts IDE