// C#
static void Main(string[] args)
{
int num1 = 1;
int num2 = 2;
// ref 변수로 참조값을 전달
Swap(ref num1, ref num2);
Console.WriteLine($"num1 : {num1}, num2 : {num2}");
}
// ref 매개 변수로 참조값을 받음
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
Output:
num1 : 2, num2 : 1
// C#
static void Main(string[] args)
{
int num1 = 1;
int num2 = 2;
int total = 100;
Add(num1, num2, out total);
Console.WriteLine($"total : {total}");
}
static void Add(int a, int b, out int total)
{
total = a + b;
}
// 기존의 값이 100이었지만, 무시되고 a + b의 값을 출력
Output:
total : 3