매개변수 (3) 썸네일형 리스트형 [C#] in 매개변수 class Program { static void Main(string[] args) { new Lesson(); } } class Lesson { public Lesson() { int num1 = 100; int num2 = 200; print(num1, num2); } // in 매개변수 int public static void print(in int num1, in int num2) { System.Console.WriteLine($"num1 : {num1}, num2 : {num2}"); } } in 1. 매개변수 앞에 in 을 붙여 주면 매개변수 전달 시 값 형식이 아닌 참조 형식으로 전달됨. 2. 매개변수 앞에 in 을 붙여 주면 해당 매개변수는 메소드 내에서 읽기만 가능. 3. 참조에 의한.. [C#] out 매개변수 class Program { static void Main(string[] args) { new Lesson(); } } class Lesson { public Lesson() { string a, b; Init(out a, out b); System.Console.WriteLine($"{a} {b}"); } // out 매개변수 string public static void Init(out string a, out string b) { a = "Good"; b = "Bad"; } } out 키워드 1. 매개변수 앞에 out 를 붙여 주면 매개변수 전달 시 값 형식이 아닌 참조 형식이으로 전달됨 2. 매개변수 앞에 out 를 붙여 주면 전달 된 매개변수는 메소드 내에서 값을 필수적으로 할당해야 함. ex.. [C#] 참조 매개변수 ref class Program { static void Main(string[] args) { new Lesson(); } } class Lesson { public Lesson() { string a = "Good", b = "Bad"; System.Console.WriteLine($"{a} {b}"); Swap(ref a, ref b); System.Console.WriteLine($"{a} {b}"); int c = 0, d = 1; System.Console.WriteLine($"{c} {d}"); Swap(ref c, ref d); System.Console.WriteLine($"{c} {d}"); } // 참조 매개변수 string public static void Swap(ref string a,.. 이전 1 다음