KO CSharp Keyword operator - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
C#: operator
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
์ฌ์ฉ์ ์ ์ ์ ํ์ ๋ํด ์ง์๋๋ ์์ ๋์์ ์ง์ ํฉ๋๋ค. ๋งค๊ฐ๋ณ์ ๊ฐ์์ ์ ์ /์ธ์คํด์ค ํ์์ ์ฐ์ฐ์์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค.
๊ฐ๋จํ ์์ (Example.cs)
์ด ์์ ๋ฅผ 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์ ์ถ๊ฐ๋ฉ๋๋ค. ==/!= ๋ฐ ํด๋น ๋น๊ต์๋ ์ ์ ํ ์๋ณ ์ ์ธ์ด ํ์ํฉ๋๋ค.
์์ 1: ๊ธฐ๋ณธ ํํ
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); }
์ 2: ๋ ๋ค๋ฅธ ์ค์ ํ์
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++; } }
์์ 3
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 ํ์ต.