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

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

C#: unsafe

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

์ง€์ •๋œ ๋ฒ”์œ„์—์„œ ํฌ์ธํ„ฐ ๊ตฌ๋ฌธ์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ปดํŒŒ์ผํ•˜๋ ค๋ฉด AllowUnsafeBlocks๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

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

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

Example.cs:

// PROJECT-EXAMPLE
unsafe
{
    int value = 3;
    int* pointer = &value;
    *pointer = 7;
    System.Console.WriteLine(value);
}

ํ”„๋กœ์ ํŠธ์—์„œ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>๋ฅผ ํ™œ์„ฑํ™”ํ•˜์„ธ์š”. ํฌ์ธํ„ฐ๋Š” ํ‘œ์‹œ๋œ ๋ฒ”์œ„ ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ:

7

ํ†ต์‚ฌ๋ก 

unsafe { Type* pointer = ...; }

.csproj์—์„œ true๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. ๊ด€๋ฆฌ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์›์‹œ ํฌ์ธํ„ฐ ์—†์ด Span๋ฅผ ํ†ตํ•ด ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด ๋” ์‰ฝ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ 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)
    {
        unsafe { int* values = stackalloc int[2]; values[0] = 3; values[1] = 7; Console.WriteLine(values[0] + values[1]); Console.WriteLine(sizeof(int)); }
    }
}

์˜ˆ 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)
    {
        int[] values = [2, 4, 6];
        unsafe { fixed (int* pointer = values) { pointer[1] = 9; Console.WriteLine(*(pointer + 1)); Console.WriteLine(sizeof(long)); } }
        Console.WriteLine(values[1]);
    }
}

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