CKTIS / C#
scope and lifetime: practical reference
scope and lifetime: practical reference: Scope determines name visibility, while lifetime determines how long a value or resource is valid.
Version: C# language and .NET 10 reference runtime
Syntax
// scope lifetimeScope determines name visibility, while lifetime determines how long a value or resource is valid.
Limit variable scope and resource ownership to the smallest required region.
A closure can extend object lifetime and captures a variable, not a value snapshot.
A ref or span cannot outlive the backing memory or escape its safe scope.
The complete program actually uses scope and lifetime; its exact expected stdout is 5.
Examples
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); }
Result
5
The complete program actually uses `scope and lifetime`; its exact expected stdout is `5`.