Repository navigation
KO CSharp Keyword fixed
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
블록 기간 동안 포인터를 통해 작업하기 위해 관리되는 데이터를 움직이지 않게 유지합니다. 고정 버퍼 — 구조체 필드의 별도 선언입니다.
이 예제를 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를 통해 작업하는 것이 더 쉽습니다.
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); }
}
}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]);
}
}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 학습.
ClassicUO — Bad Newbie & C# Scripts IDE