CKTIS / C#
fixed: практическа справка
fixed: практическа справка: fixed закрепва управляваната памет и дава стабилен указател в ограничен обхват.
Версия: C# language; verified against C# 14 reference
Синтаксис
fixed /* grammar-specific form */fixed закрепва управляваната памет и дава стабилен указател в ограничен обхват.
Използвайте само за кратък interop или unsafe алгоритъм, изискващ адрес.
Продължителният pinning фрагментира heap, а указателят не бива да напуска блока.
Празен span може да даде null указател; кодът изисква unsafe и валиден тип елемент.
Пълната програма действително използва fixed; точният очакван stdout е 7.
Примери
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); }
Резултат
7
Пълната програма действително използва `fixed`; точният очакван stdout е `7`.