Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / C#

fixed: praktična referenca

fixed: praktična referenca: fixed prikvačuje upravljanu memoriju i u ograničenom opsegu daje stabilan pokazivač.

Verzija: C# language; verified against C# 14 reference

Sintaksa

fixed /* grammar-specific form */

fixed prikvačuje upravljanu memoriju i u ograničenom opsegu daje stabilan pokazivač.

Koristite samo za kratak interop ili unsafe algoritam koji treba adresu.

Dug pinning fragmentira heap, a pokazivač ne smije izaći iz bloka.

Prazan span može dati pokazivač null; kod zahtijeva unsafe i valjan tip elementa.

Cijeli program doista koristi fixed; točno očekivani stdout jest 7.

Primjeri

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

Rezultat

7

Cijeli program doista koristi `fixed`; točno očekivani stdout jest `7`.

Izvori