KO CSharp Keyword fixed - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
C#: fixed
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
๋ธ๋ก ๊ธฐ๊ฐ ๋์ ํฌ์ธํฐ๋ฅผ ํตํด ์์ ํ๊ธฐ ์ํด ๊ด๋ฆฌ๋๋ ๋ฐ์ดํฐ๋ฅผ ์์ง์ด์ง ์๊ฒ ์ ์งํฉ๋๋ค. ๊ณ ์ ๋ฒํผ โ ๊ตฌ์กฐ์ฒด ํ๋์ ๋ณ๋ ์ ์ธ์ ๋๋ค.
๊ฐ๋จํ ์์ (Example.cs)
์ด ์์ ๋ฅผ Example.cs๋ก ๋ฐ๋ก ์ ์ฅํ๊ณ ์ด ํ์ด์ง์ ๋ค๋ฅธ ์์ ์ ๋
๋ฆฝ์ ์ผ๋ก ์คํํ์ธ์.
Example.cs:
// PROJECT-EXAMPLE
int[] values = [3];
unsafe
{
fixed (int* pointer = values) *pointer = 7;
}
System.Console.WriteLine(values[0]);
ํ๋ก์ ํธ์์ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>๋ฅผ ํ์ฑํํ์ธ์. ํฌ์ธํฐ๋ ํ์๋ ๋ฒ์ ๋ด์์๋ง ์ ํจํฉ๋๋ค.
๊ฒฐ๊ณผ:
7
ํต์ฌ๋ก
fixed (Type* pointer = managedValue) { ... }
.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)
{
string text = "ore";
unsafe { fixed (char* pointer = text) Console.WriteLine(*pointer); }
}
}
์ 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]);
}
}
์์ 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)
{
unsafe { Packet packet = default; packet.Bytes[0] = 7; Console.WriteLine(packet.Bytes[0]); }
}
}
public unsafe struct Packet { public fixed byte Bytes[4]; }
๊ณต์ ์ถ์ฒ: Microsoft ํ์ต.