Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

fixed: referencia práctica

fixed: referencia práctica: fixed fija memoria administrada y expone un puntero estable durante un ámbito limitado.

Versión: C# language; verified against C# 14 reference

Sintaxis

fixed /* grammar-specific form */

fixed fija memoria administrada y expone un puntero estable durante un ámbito limitado.

Úsalo solo para interop breve o un algoritmo unsafe que requiera una dirección.

Un pinning largo fragmenta el heap y el puntero no debe escapar del bloque.

Un span vacío puede producir un puntero null; el código requiere unsafe y un tipo de elemento válido.

El programa completo usa realmente fixed; su stdout esperado exacto es 7.

Ejemplos

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); }

Resultado

7

El programa completo usa realmente `fixed`; su stdout esperado exacto es `7`.

Fuentes