KO CSharp Keyword ref - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
C#: ref
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
๋ณ์์ ๋ํ ์ฐธ์กฐ๋ฅผ ์ ๋ฌํ๊ฑฐ๋ ๋ฐํํฉ๋๋ค. ref local, ref ํ๋ ๋ฐ ref ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธํฉ๋๋ค. ์๋ช ๊ท์น์์๋ ์ง์ญ ๋ณ์์ ๋ํ ์ฐธ์กฐ๋ฅผ ๋ฐํํ๋ ๊ฒ์ ํ์ฉํ์ง ์์ต๋๋ค.
๊ฐ๋จํ ์์ (Example.cs)
์ด ์์ ๋ฅผ 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 Type
์์ 1: ๊ธฐ๋ณธ ํํ
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;
Values.Increment(ref number);
Console.WriteLine(number);
}
}
public static class Values { public static void Increment(ref int value) { value++; } }
์ 2: ๋ ๋ค๋ฅธ ์ค์ ํ์
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]; }
์์ 3
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}");
}
}
์์ 4
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 ํ์ต.