Repository navigation
FR CSharp Keyword fixed
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Pendant toute la durée du blocage, il maintient les données gérées immobiles pour pouvoir utiliser le pointeur ; tampon fixe — une déclaration distincte d'un champ de structure.
Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.
Example.cs:
// PROJECT-EXAMPLE
int[] values = [3];
unsafe
{
fixed (int* pointer = values) *pointer = 7;
}
System.Console.WriteLine(values[0]);Activez <AllowUnsafeBlocks>true</AllowUnsafeBlocks> dans le projet. Le pointeur est valide uniquement dans la portée indiquée.
Résultat:
7
fixed (Type* pointer = managedValue) { ... }Dans .csproj, définissez true. Pour les données gérées, il est généralement plus facile de travailler via Span sans pointeurs bruts.
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]; }Source officielle : Microsoft Apprendre.
ClassicUO — Bad Newbie & C# Scripts IDE