Repository navigation
KO CSharp Keyword scoped
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
범위는 참조 또는 참조 유사 매개변수 값의 유효한 수명을 제한합니다. 컴파일러는 설정된 수명을 초과하는 방식으로 이러한 참조가 반환되는 것을 허용하지 않습니다. 수정자는 데이터를 불변으로 만들지 않습니다. 범위가 지정된 참조를 사용하면 호출자의 값을 변경할 수 있습니다.
이 예제를 Example.cs로 따로 저장하고 이 페이지의 다른 예제와 독립적으로 실행하세요.
Example.cs:
// PROJECT-EXAMPLE
void Add(scoped ref int value) { value += 2; }
int count = 3;
Add(ref count);
System.Console.WriteLine(count);결과:
5
static int Sum(scoped ReadOnlySpan<int> values);
static void Add(scoped ref int value, int amount);using System;
internal static class Program
{
public static void Main()
{
ReadOnlySpan<int> readings = stackalloc int[] { 3, 5, 7 };
Console.WriteLine(Sum(readings));
}
private static int Sum(scoped ReadOnlySpan<int> values)
{
int total = 0;
foreach (int value in values) total += value;
return total;
}
}결과:
15
using System;
internal static class Program
{
public static void Main()
{
int points = 10;
Add(ref points, 4);
Add(ref points, 6);
Console.WriteLine(points);
}
private static void Add(scoped ref int value, int amount)
{
value += amount;
}
}결과:
20
공식 출처: Microsoft 학습.
ClassicUO — Bad Newbie & C# Scripts IDE