값 형식 매개 변수 전달(C# 프로그래밍 가이드)

업데이트: 2007년 11월

값 형식 변수는 데이터에 대한 참조를 포함하는 참조 형식 변수와는 반대로 데이터를 직접 포함합니다. 따라서 메서드에 값-형식 변수를 전달하는 것은 메서드에 변수의 복사본을 전달하는 것을 의미합니다. 메서드에서 매개 변수를 변경해도 변수에 저장되어 있는 원래 데이터에 영향을 주지는 않습니다. 호출한 메서드로 매개 변수 값을 변경하려면 ref 또는 out 키워드를 사용하여 매개 변수를 참조로 전달해야 합니다. 편의상 다음 예제에서는 ref를 사용합니다.

예제

다음 예제에서는 값으로 값-형식 매개 변수를 전달하는 것에 대해 설명합니다. 변수 n는 값으로 메서드 SquareIt에 전달됩니다. 메서드 안에서의 변경 사항은 변수의 원래 값에 영향을 주지 않습니다.

class PassingValByVal
{
    static void SquareIt(int x)
    // The parameter x is passed by value.
    // Changes to x will not affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine("The value before calling the method: {0}", n);

        SquareIt(n);  // Passing the variable by value.
        System.Console.WriteLine("The value after calling the method: {0}", n);

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    The value before calling the method: 5
    The value inside the method: 25
    The value after calling the method: 5
*/

변수 n은 값 형식으로 데이터(값 5)를 포함합니다. SquareIt를 호출하면 n의 내용은 매개 변수 x에 복사되어 메서드에서 제곱됩니다. 그러나 Main에서 n의 값은 SquareIt 메서드 호출 전과 후 모두 같습니다. 실제로 메서드 안에서의 변경 사항은 지역 변수 x에만 영향을 줍니다.

다음 예제는 ref 키워드를 사용하여 매개 변수를 전달한다는 점을 제외하고 이전 예제와 같습니다. 매개 변수 값은 메서드 호출 후 변경됩니다.

class PassingValByRef
{
    static void SquareIt(ref int x)
    // The parameter x is passed by reference.
    // Changes to x will affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine("The value before calling the method: {0}", n);

        SquareIt(ref n);  // Passing the variable by reference.
        System.Console.WriteLine("The value after calling the method: {0}", n);

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    The value before calling the method: 5
    The value inside the method: 25
    The value after calling the method: 25
*/

이 예제에서 전달되는 것은 n의 값이 아니라 n에 대한 참조입니다. 매개 변수 x는 int가 아니라 int에 대한 참조(이 경우에는 n에 대한 참조)입니다. 따라서 메서드 안에서 x를 제곱하면 실제로 제곱되는 것은 x가 참조하는 n입니다.

전달된 매개 변수 값을 변경하는 일반적인 예는 두 변수 x와 y를 전달하고 메서드로 내용을 맞바꾸는 Swap 메서드입니다. 참조로 매개 변수를 Swap 메서드에 전달해야 하며 그렇지 않으면 메서드 안에서 매개 변수의 로컬 복사본을 처리하게 됩니다. 다음은 참조 매개 변수를 사용하는 Swap 메서드의 예제입니다.

static void SwapByRef(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}

이 메서드를 호출할 때 다음과 같이 호출에 ref 키워드를 사용합니다.

static void Main()
{
    int i = 2, j = 3;
    System.Console.WriteLine("i = {0}  j = {1}" , i, j);

    SwapByRef (ref i, ref j);

    System.Console.WriteLine("i = {0}  j = {1}" , i, j);

    // Keep the console window open in debug mode.
    System.Console.WriteLine("Press any key to exit.");
    System.Console.ReadKey();
}
/* Output:
    i = 2  j = 3
    i = 3  j = 2
*/

참고 항목

개념

C# 프로그래밍 가이드

참조

매개 변수 전달(C# 프로그래밍 가이드)

참조 형식 매개 변수 전달(C# 프로그래밍 가이드)