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