CKTIS / C#
scope and lifetime: praktična referenca
scope and lifetime: praktična referenca: Scope določa vidnost imena, lifetime pa veljavnost vrednosti ali vira.
Različica: C# language and .NET 10 reference runtime
Sintaksa
// scope lifetimeScope določa vidnost imena, lifetime pa veljavnost vrednosti ali vira.
Omejite scope spremenljivke in lastništvo virov na najmanjše potrebno območje.
Closure lahko podaljša življenje objekta in zajame spremenljivko, ne posnetka vrednosti.
Ref ali span ne sme preživeti osnovnega pomnilnika niti uiti iz varnega scope.
Celoten program dejansko uporabi scope and lifetime; natančno pričakovani stdout je 5.
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;
var outer = 2; { var inner = 3; Console.WriteLine(outer + inner); }
Rezultat
5
Celoten program dejansko uporabi `scope and lifetime`; natančno pričakovani stdout je `5`.