본문 바로가기

C#

[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. 참조에 의한 읽기 기능을 제공함으로써 메모리 복사를 줄이고 성능 향상에 목적이 있음.

ex)

void method(in int a)

void method(in string a)

void method(in ushort a)

...

 

 

2023.10.10 - [C#] - [C#] out 매개변수

 

[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

bysik1109.tistory.com

 

2023.10.10 - [C#] - [C#] 참조 매개변수 ref

 

[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.Cons

bysik1109.tistory.com

 

 

'C#' 카테고리의 다른 글

[C#] 문자열 배열 분할 Split (char 분할, string 분할, StringSplitOptions)  (1) 2023.10.23
[C#] 매개변수 배열 (params)  (0) 2023.10.11
[C#] out 매개변수  (2) 2023.10.10
[C#] 참조 매개변수 ref  (0) 2023.10.10
[C#] 파일 삭제  (0) 2023.10.07