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