CKTIS / C#
scope and lifetime: praktická referencia
scope and lifetime: praktická referencia: Scope určuje viditeľnosť názvu a lifetime dobu platnosti hodnoty alebo prostriedku.
Verzia: C# language and .NET 10 reference runtime
Syntax
// scope lifetimeScope určuje viditeľnosť názvu a lifetime dobu platnosti hodnoty alebo prostriedku.
Obmedzte scope premennej a vlastníctvo prostriedkov na najmenšiu potrebnú oblasť.
Closure môže predĺžiť život objektu a zachytí premennú, nie snímku hodnoty.
Ref alebo span nesmie prežiť podkladovú pamäť ani uniknúť z bezpečného scope.
Úplný program skutočne používa scope and lifetime; presne očakávaný stdout je 5.
Prí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 outer = 2; { var inner = 3; Console.WriteLine(outer + inner); }
Výsledok
5
Úplný program skutočne používa `scope and lifetime`; presne očakávaný stdout je `5`.