C#
C# Study / 값(value) 타입, 참조(reference) 타입 변수 실습 및 관찰
universedevelope
2022. 8. 1. 18:28
class Program
{
public static void Swap(int a, int b)
{
int temp = b;
b = a;
a = temp;
}
public static void Swaped(ref int a, ref int b)
{
int temp = b;
b = a;
a = temp;
}
static void Main(string[] args)
{
// 참조에 의한 매개변수 전달..
// 참조는 원래의 값이 바뀌지 않음
int x = 3;
int y = 4;
Console.WriteLine($"x:{x}, y:{y}");
Swap(x, y);
Console.WriteLine($"x:{x}, y:{y}");
/*
x:3, y:4
x:3, y:4
*/
Swaped(ref x, ref y);
Console.WriteLine($"x:{x}, y:{y}");
/*
x:3, y:4
x:3, y:4
x:4, y:3
*/
// Divide 메소드 실행
int k = 20;
int j = 3;
int quotient = 0;
int remainder = 0;
Divide(k, j, ref quotient, ref remainder);
Console.WriteLine("Quotient : {0}, Remainder : {1}", quotient, remainder);
// Quotient : 6, Remainder : 2
// Sum 메소드 실행
int sumResult = Sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Console.WriteLine(sumResult);
//55
}
}
728x90