Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

scope and lifetime: referência prática

scope and lifetime: referência prática: O scope determina a visibilidade do nome e lifetime a validade de um valor ou recurso.

Versão: C# language and .NET 10 reference runtime

Sintaxe

// scope lifetime

O scope determina a visibilidade do nome e lifetime a validade de um valor ou recurso.

Limita o scope das variáveis e propriedade dos recursos à menor região necessária.

Uma closure pode prolongar a vida do objeto e captura uma variável, não uma fotografia do valor.

Um ref ou span não pode sobreviver à memória base nem escapar do scope seguro.

O programa completo usa realmente scope and lifetime; o stdout exato esperado é 5.

Exemplos

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); }

Resultado

5

O programa completo usa realmente `scope and lifetime`; o stdout exato esperado é `5`.

Fontes