参照型のパラメーターの引き渡し (C# プログラミング ガイド)

参照型の変数には、データが直接格納されず、データへの参照が格納されます。 参照型のパラメーターを値で渡すときには、クラス メンバーの値など、参照によって指されるデータを変更できます。 ただし、参照自身の値は変更できません。つまり、同じ参照を使用して、新しいクラスのメモリを割り当て、ブロックの外で永続化させることはできません。 参照自身の値を変更するには、ref キーワードまたは out キーワードを使用してパラメーターを渡します。 説明を簡単にするために、次の例では ref だけを使用しています。

使用例

参照型のパラメーター arr を値で Change メソッドに渡す方法を、次の例で示します。 パラメーターは arr への参照であるため、配列要素の値を変更できます。 ただし、他のメモリ位置へのパラメーターの再割り当ては、メソッド内だけで有効です。元の変数 arr には影響しません。

class PassingRefByVal 
{
    static void Change(int[] pArray)
    {
        pArray[0] = 888;  // This change affects the original element.
        pArray = new int[5] {-3, -1, -2, -3, -4};   // This change is local.
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }

    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);

        Change(arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);
    }
}
/* Output:
    Inside Main, before calling the method, the first element is: 1
    Inside the method, the first element is: -3
    Inside Main, after calling the method, the first element is: 888
*/

上の例では、参照型の配列 arr は、ref パラメーターを指定せずにメソッドに渡されています。 このような場合は、arr を指す参照のコピーがメソッドに渡されます。 出力は、メソッドが配列要素の内容を (この場合は 1 から 888 へ) 変更できることを示しています。 ただし、Change メソッド内で new 演算子を使用して新しいメモリ領域を割り当てると、変数 pArray は新しい配列を参照します。 したがって、新しい領域の割り当ての後に変更があっても、Main 内で作成されている元の配列 arr は影響を受けません。 この例では Main メソッド内と Change メソッド内で 2 つの配列が作成されています。

メソッド ヘッダーと呼び出しで ref キーワードを使用していることを除けば、この例は前の例と同じです。 メソッド内での変更は、呼び出しプログラム内の元の変数に影響します。

class PassingRefByRef 
{
    static void Change(ref int[] pArray)
    {
        // Both of the following changes will affect the original variables:
        pArray[0] = 888;
        pArray = new int[5] {-3, -1, -2, -3, -4};
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }

    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);

        Change(ref arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
    }
}
/* Output:
    Inside Main, before calling the method, the first element is: 1
    Inside the method, the first element is: -3
    Inside Main, after calling the method, the first element is: -3
*/

メソッド内でのすべての変更が、Main 内の元の配列に影響します。 実際、元の配列は new 演算子を使用して再割り当てされています。 つまり、Change メソッドの呼び出し後は、arr へのすべての参照が、Change メソッドで作成された 5 つの要素を持つ配列を指しています。

文字列の交換は、参照によって参照型のパラメーターを渡す方法の良い例です。 次の例では、str1 と str2 の 2 つの文字列が Main で初期化され、ref キーワードを使用してパラメーターとして SwapStrings メソッドに渡されています。 2 つの文字列はこのメソッド内で交換され、Main 内でもその結果が反映されています。

 class SwappingStrings
 {
     static void SwapStrings(ref string s1, ref string s2)
     // The string parameter is passed by reference.
     // Any changes on parameters will affect the original variables.
     {
         string temp = s1;
         s1 = s2;
         s2 = temp;
         System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
     }

     static void Main()
     {
         string str1 = "John";
         string str2 = "Smith";
         System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

         SwapStrings(ref str1, ref str2);   // Passing strings by reference
         System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
     }
 }
 /* Output:
     Inside Main, before swapping: John Smith
     Inside the method: Smith John
     Inside Main, after swapping: Smith John
*/

この例では、呼び出しプログラムの変数に変更を反映するために、パラメーターを参照で渡す必要があります。 メソッド ヘッダーとメソッド呼び出しの両方から ref キーワードを削除すると、呼び出しプログラムには変更が反映されません。

文字列の詳細については、「string (C# リファレンス)」を参照してください。

参照

参照

パラメーターの引き渡し (C# プログラミング ガイド)

ref と out を使用した配列の引き渡し (C# プログラミング ガイド)

ref (C# リファレンス)

参照型 (C# リファレンス)

概念

C# プログラミング ガイド