Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / C#

fixed: praktična referenca

fixed: praktična referenca: fixed pripne upravljani pomnilnik in v omejenem obsegu ponudi stabilen kazalec.

Različica: C# language; verified against C# 14 reference

Sintaksa

fixed /* grammar-specific form */

fixed pripne upravljani pomnilnik in v omejenem obsegu ponudi stabilen kazalec.

Uporabite le za kratek interop ali unsafe algoritem, ki potrebuje naslov.

Dolgotrajen pinning drobi kopico, kazalec pa ne sme uiti iz bloka.

Prazen span lahko da kazalec null; koda zahteva unsafe in veljaven tip elementa.

Celoten program dejansko uporabi fixed; natančno pričakovani stdout je 7.

Primeri

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

Celoten program dejansko uporabi `fixed`; natančno pričakovani stdout je `7`.

Viri