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 ํ•™์Šต.