CKTIS / C#
fixed: practical reference
fixed: practical reference: fixed pins managed memory and exposes a stable pointer for a limited scope.
Version: C# language; verified against C# 14 reference
Syntax
fixed /* grammar-specific form */fixed pins managed memory and exposes a stable pointer for a limited scope.
Use it only for short interop or an unsafe algorithm that requires an address.
Long pinning fragments the heap, and the pointer must not escape the block.
An empty span may yield a null pointer; the code requires unsafe and a valid element type.
The complete program actually uses fixed; its exact expected stdout is 7.
Examples
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
unsafe { var values = new[] { 7 }; fixed (int* pointer = values) Console.WriteLine(*pointer); }
Result
7
The complete program actually uses `fixed`; its exact expected stdout is `7`.