🌸 out
- ref와 달리 메소드가 해당 매개변수에 결과를 저장하지 않으면 에러가 발생한다.
- 출력 전용 매개변수는 즉석으로 선언할 수 있다.
out 코드
using System;
namespace UsingOut
{
class MainApp
{
static void Divide(int a, int b, out int quotient, out int remainder)
{
quotient = a / b;
remainder = a % b;
}
static void Main(string[] args)
{
int a = 20;
int b = 3;
Divide(a, b, out int c, out int d);
Console.WriteLine($"{a} / {b} = {c} ... {d}");
}
}
}
[실행 결과]
20 / 3 = 6 ... 2
ref 코드
using System;
namespace UsingRef
{
class MainApp
{
static void Divide (int a, int b, ref int quotient, ref int remainder)
{
quotient = a / b;
remainder = a % b;
}
static void Main (string[] args)
{
int a = 20;
int b = 3;
int c = 0;
int d = 0;
Divide(a, b, ref c, ref d);
Console.WriteLine($"{a} / {b} = {c} ... {d}");
}
}
}
[실행 결과]
20 / 3 = 6 ... 2