База знанияИзпитен център
BGБългарски
Избери език13

CKTIS / C#

unsafe: практическа справка

unsafe: практическа справка: unsafe позволява указатели и операции извън пълната защита на паметта от CLR.

Версия: C# language; verified against C# 14 reference

Синтаксис

unsafe /* grammar-specific form */

unsafe позволява указатели и операции извън пълната защита на паметта от CLR.

Ограничете го до минимална изолирана област за interop или измерена оптимизация.

Дефект може да повреди паметта и процеса; компилацията изисква AllowUnsafeBlocks.

Животът, pinning, подравняването и границите трябва да се докажат ръчно.

Пълната програма действително използва unsafe; точният очакван 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 value = 7; var pointer = &value; Console.WriteLine(*pointer); }

Резултат

7

Пълната програма действително използва `unsafe`; точният очакван stdout е `7`.

Източници