Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

scope and lifetime: referencia práctica

scope and lifetime: referencia práctica: El ámbito determina la visibilidad del nombre y lifetime cuánto tiempo es válido un valor o recurso.

Versión: C# language and .NET 10 reference runtime

Sintaxis

// scope lifetime

El ámbito determina la visibilidad del nombre y lifetime cuánto tiempo es válido un valor o recurso.

Limita el ámbito de variables y propiedad de recursos a la región mínima necesaria.

Una closure puede prolongar la vida del objeto y captura una variable, no una instantánea del valor.

Un ref o span no puede sobrevivir a la memoria base ni escapar de su ámbito seguro.

El programa completo usa realmente scope and lifetime; su stdout esperado exacto es 5.

Ejemplos

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

El programa completo usa realmente `scope and lifetime`; su stdout esperado exacto es `5`.

Fuentes