CKTIS / C#
scope and lifetime: riferimento pratico
scope and lifetime: riferimento pratico: Lo scope determina la visibilità del nome e lifetime la validità di valore o risorsa.
Versione: C# language and .NET 10 reference runtime
Sintassi
// scope lifetimeLo scope determina la visibilità del nome e lifetime la validità di valore o risorsa.
Limita scope delle variabili e proprietà delle risorse alla regione minima necessaria.
Una closure può estendere la vita dell’oggetto e cattura una variabile, non uno snapshot del valore.
Un ref o span non può sopravvivere alla memoria sottostante né uscire dallo scope sicuro.
Il programma completo usa realmente scope and lifetime; lo stdout esatto atteso è 5.
Esempi
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 outer = 2; { var inner = 3; Console.WriteLine(outer + inner); }
Risultato
5
Il programma completo usa realmente `scope and lifetime`; lo stdout esatto atteso è `5`.