CKTIS / C#
ref: praktická reference
ref: praktická reference: ref předává nebo vrací alias na existující úložiště místo kopie.
Verze: C# language; verified against C# 14 reference
Syntaxe
ref /* grammar-specific form */ref předává nebo vrací alias na existující úložiště místo kopie.
Použijte pro efektivní mutaci velkých structs a API zaměřená na span s řízeným lifetime.
Alias může porušit invarianty a ztížit analýzu; nesmí přežít svůj referent.
ref local musí být inicializována referencí a ref return podléhá escape analysis.
Úplný program skutečně používá ref; přesně očekávaný stdout je 7.
Příklady
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;
var value = 2; ref var alias = ref value; alias = 7; Console.WriteLine(value);
Výsledek
7
Úplný program skutečně používá `ref`; přesně očekávaný stdout je `7`.