Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / C#

scope and lifetime: praktická reference

scope and lifetime: praktická reference: Scope určuje viditelnost jména a lifetime dobu platnosti hodnoty nebo prostředku.

Verze: C# language and .NET 10 reference runtime

Syntaxe

// scope lifetime

Scope určuje viditelnost jména a lifetime dobu platnosti hodnoty nebo prostředku.

Omezte scope proměnné a vlastnictví prostředků na nejmenší potřebnou oblast.

Closure může prodloužit život objektu a zachytí proměnnou, nikoli snímek hodnoty.

Ref nebo span nesmí přežít podkladovou paměť ani uniknout z bezpečného scope.

Úplný program skutečně používá scope and lifetime; přesně očekávaný stdout je 5.

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 outer = 2; { var inner = 3; Console.WriteLine(outer + inner); }

Výsledek

5

Úplný program skutečně používá `scope and lifetime`; přesně očekávaný stdout je `5`.

Zdroje