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

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

C#: safe

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

์—…๋ฐ์ดํŠธ๋œ ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์ „ ๋ชจ๋ธ์˜ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ˆ˜์ •์ž: ์ž‘์„ฑ์ž๋Š” ํ˜ธ์ถœ์ž์˜ ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์ปจํ…์ŠคํŠธ๋ฅผ ์š”๊ตฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ์„ ์–ธ์„ ๋ช…์‹œ์ ์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

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

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

Example.cs:

// PROJECT-EXAMPLE
class ManagedValue { public int Count { get; set; } }

์ด ์งง์€ ์˜ˆ์ œ๋Š” C# 14๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์ง€์›๋˜์ง€ ์•Š๋Š” ๋ฏธ๋ฆฌ ๋ณด๊ธฐ ๊ตฌ๋ฌธ safe๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ†ต์‚ฌ๋ก 

public static safe extern int Method();

C# 15 / .NET 11์˜ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ž…๋‹ˆ๋‹ค. ๊ณต์‹ ๊ทœ์น™์—์„œ๋Š” LangVersion=๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์š”๊ตฌํ•˜๊ณ  ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•ด ์—…๋ฐ์ดํŠธ๋œ ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์ „ ๊ทœ์น™์„ ๋ณ„๋„๋กœ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ C# 14์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์„ ์–ธ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ƒํƒœ: ๋ฏธ๋ฆฌ๋ณด๊ธฐ; ํ˜„์žฌ ๊ตฌ์„ฑ C# 14 / .NET 10๋Š” ์ด๋Ÿฌํ•œ ์˜ˆ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ 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(Native.GetCurrentProcessId());
    }
}
public static class Native { [DllImport("kernel32.dll")] public static safe extern uint GetCurrentProcessId(); }

์˜ˆ 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)
    {
        Union value = new() { Integer = 1065353216 };
        Console.WriteLine(value.Real);
    }
}
[StructLayout(LayoutKind.Explicit)] public struct Union { [FieldOffset(0)] public safe int Integer; [FieldOffset(0)] public safe float Real; }

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