CKTIS / C#
scoped: practical reference
scoped: practical reference: scoped prevents a ref or span from escaping the safe lifetime of a call.
Version: C# language; verified against C# 14 reference
Syntax
scoped /* grammar-specific form */scoped prevents a ref or span from escaping the safe lifetime of a call.
Use it in low-level APIs so the compiler enforces lifetime boundaries.
It neither owns memory nor extends the lifetime of the referenced buffer.
An unsafe assignment or return fails the compiler's escape analysis.
The complete program actually uses scoped; 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;
Span<int> values = stackalloc[] { 5 }; Console.WriteLine(Read(values)); static int Read(scoped ReadOnlySpan<int> values) => values[0];
Result
5
The complete program actually uses `scoped`; its exact expected stdout is `5`.