Repository navigation
KO CSharp Keyword ref
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
변수에 대한 참조를 전달하거나 반환합니다. ref local, ref 필드 및 ref 구조체를 선언합니다. 수명 규칙에서는 지역 변수에 대한 참조를 반환하는 것을 허용하지 않습니다.
이 예제를 Example.cs로 따로 저장하고 이 페이지의 다른 예제와 독립적으로 실행하세요.
Example.cs:
// PROJECT-EXAMPLE
void Add(ref int value) { value += 2; }
int count = 3;
Add(ref count);
System.Console.WriteLine(count);결과:
5
Method(ref value) / ref int alias = ref value / ref struct Typeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
int number = 3;
Values.Increment(ref number);
Console.WriteLine(number);
}
}
public static class Values { public static void Increment(ref int value) { value++; } }using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
int[] values = [2, 4];
ref int first = ref Arrays.First(values);
first = 9;
Console.WriteLine(values[0]);
}
}
public static class Arrays { public static ref int First(int[] values) => ref values[0]; }using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
int left = 2, right = 4;
ref int selected = ref (left > right ? ref left : ref right);
selected += 10;
Console.WriteLine($"{left}:{right}");
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
int number = 3;
Reference holder = new(ref number);
holder.Value = 9;
Console.WriteLine(number);
}
}
public ref struct Reference { public ref int Value; public Reference(ref int value) { Value = ref value; } }공식 출처: Microsoft 학습.
ClassicUO — Bad Newbie & C# Scripts IDE