CKTIS / C#
ref: référence pratique
ref: référence pratique: ref transmet ou renvoie un alias vers un emplacement existant plutôt qu’une copie.
Version : C# language; verified against C# 14 reference
Syntaxe
ref /* grammar-specific form */ref transmet ou renvoie un alias vers un emplacement existant plutôt qu’une copie.
Utilisez-le pour muter efficacement de grandes structs et des API orientées span à lifetime contrôlée.
Un alias peut rompre les invariants et compliquer le raisonnement ; il ne doit pas survivre au référent.
Une ref local doit être initialisée par référence et ref return suit l’escape analysis.
Le programme complet utilise réellement ref ; sa sortie stdout exacte attendue est 7.
Exemples
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);
Résultat
7
Le programme complet utilise réellement `ref` ; sa sortie stdout exacte attendue est `7`.