KO CSharp Keyword extension - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki

ะฃะบั€ะฐั—ะฝััŒะบะฐ ยท ะ ัƒััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซ”ไธญๆ–‡ ยท ๆ—ฅๆœฌ่ชž ยท ํ•œ๊ตญ์–ด

C#: extension

ํ•ธ๋“œ๋ถ C# ยท ์‹ค์šฉ ๊ฐ€์ด๋“œ

ํ™•์žฅ ๋ธ”๋ก(๊ธฐ์กด ์œ ํ˜•์— ๋Œ€ํ•ด ๋ฉ”์„œ๋“œ, ์†์„ฑ ๋ฐ ๊ธฐํƒ€ ์ง€์›๋˜๋Š” ๋ฉค๋ฒ„)์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ (Example.cs)

์ด ์˜ˆ์ œ๋ฅผ Example.cs๋กœ ๋”ฐ๋กœ ์ €์žฅํ•˜๊ณ  ์ด ํŽ˜์ด์ง€์˜ ๋‹ค๋ฅธ ์˜ˆ์ œ์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰ํ•˜์„ธ์š”.

Example.cs:

// PROJECT-EXAMPLE
System.Console.WriteLine("ore".Size);
static class TextTools
{
    extension(string text) { public int Size => text.Length; }
}

๊ฒฐ๊ณผ:

3

ํ†ต์‚ฌ๋ก 

extension(Type receiver) { public ReturnType Member ... }

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)
    {
        Console.WriteLine("ore".Size);
    }
}
public static class TextTools { extension(string text) { public int Size => text.Length; } }

์˜ˆ 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)
    {
        Console.WriteLine((12).IsEven());
    }
}
public static class NumberTools { extension(int value) { public bool IsEven() => value % 2 == 0; } }

๊ณต์‹ ์ถœ์ฒ˜: Microsoft ํ•™์Šต.