http://dis.dankook.ac.kr/lectures/hci10/entry/pass-by-value와-pass-by-reference와-pass-by-output-비교
pass value type by value (값형식을 값에 의한 전달)
static void Square1(int x)
{
x *= x;
Console.WriteLine(“The value inside the method: {0}”, x);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
Square1(i);
Console.WriteLine(“i={0}”, i);
}
{
x *= x;
Console.WriteLine(“The value inside the method: {0}”, x);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
Square1(i);
Console.WriteLine(“i={0}”, i);
}
i=5
The value inside the method: 25
i=5
The value inside the method: 25
i=5
pass reference type by value (참조형식을 값에 의한 전달)
-copy of reference가 전달
static void ChangeArray1(int[] arr)
{
arr[0]=888; // 원본 배열의 첫번째 값은 888로 변경
arr = new int[5] {-3, -1, -2, -3, -4}; // local 변수 재지정
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray1(myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}
{
arr[0]=888; // 원본 배열의 첫번째 값은 888로 변경
arr = new int[5] {-3, -1, -2, -3, -4}; // local 변수 재지정
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray1(myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}
myArray[0]=1
The value inside the method: arr[0]=-3
myArray[0]=888
The value inside the method: arr[0]=-3
myArray[0]=888
pass value type by reference (값형식을 참조에 의한 전달)
–ref 키워드 사용
static void Square2(ref int x)
{
x *= x;
Console.WriteLine(“The value inside the method: {0}”, x);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
Square2(ref i);
Console.WriteLine(“i={0}”, i);
}
{
x *= x;
Console.WriteLine(“The value inside the method: {0}”, x);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
Square2(ref i);
Console.WriteLine(“i={0}”, i);
}
i=5
The value inside the method: 25
i=25
The value inside the method: 25
i=25
pass reference type by reference (참조형식을 참조에 의한 전달)
–ref 키워드 사용
static void ChangeArray2(ref int[] arr)
{
arr[0]=888; // 원본 배열의 첫번째 값은 888로 변경
arr = new int[5] {-3, -1, -2, -3, -4}; // 원본 배열이 다시 변경
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray2(ref myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}
{
arr[0]=888; // 원본 배열의 첫번째 값은 888로 변경
arr = new int[5] {-3, -1, -2, -3, -4}; // 원본 배열이 다시 변경
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray2(ref myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}
myArray[0]=1
The value inside the method: arr[0]=-3
myArray[0]=-3
The value inside the method: arr[0]=-3
myArray[0]=-3
pass value type by output (값형식을 output에 의한 전달)
–out 키워드 사용
static void Square3(int x, out int result)
{
result = x*x;
Console.WriteLine(“The value inside the method: {0}”, result);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
int result;
Square3(i, out result);
Console.WriteLine(“result={0}”, result);
}
{
result = x*x;
Console.WriteLine(“The value inside the method: {0}”, result);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
int result;
Square3(i, out result);
Console.WriteLine(“result={0}”, result);
}
i=5
The value inside the method: 25
result=25
The value inside the method: 25
result=25
pass reference type by output (참조형식을 output에 의한 전달)
–out 키워드 사용
static void ChangeArray3(out int[] arr)
{
//arr[0]=888; // use of unassigned out parameter ‘arr’ ERROR
arr = new int[5] {-3, -1, -2, -3, -4}; // 원본 배열이 변경
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray3(out myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}
{
//arr[0]=888; // use of unassigned out parameter ‘arr’ ERROR
arr = new int[5] {-3, -1, -2, -3, -4}; // 원본 배열이 변경
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray3(out myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}
myArray[0]=1
The value inside the method: arr[0]=-3
myArray[0]=-3
The value inside the method: arr[0]=-3
myArray[0]=-3