参照タイプパラメータの転送

7455 ワード

参照タイプの変数は、そのデータを直接含まない.データへの参照が含まれています.参照タイプのパラメータを値で渡すと、クラスメンバーの値などの参照が指すデータが変更される可能性があります.ただし、参照自体の値は変更できません.すなわち、同じ参照を使用して新しいクラスにメモリを割り当て、ブロック外で保持することはできません.そうするには、refまたはoutのキーワードを使用してパラメータを渡す必要があります.簡単にするために、以下の例ではrefを使用します.
例:参照タイプを値で渡す
次の例では、参照タイプのパラメータarrを値によってChangeメソッドに渡す例を示します.このパラメータはarrへの参照であるため、配列要素の値を変更する可能性があります.ただし、パラメータを異なるメモリ位置に再割り当てしようとすると、この操作はメソッド内でのみ有効であり、元の変数arrには影響しません.
C#
 
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]);

    }

}




しゅつりょく
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に新しい配列を参照させます.したがって、その後の変更は元の配列arr(Main内で作成された)には影響しません.実際には、この例では2つの配列が作成されています.1つはMain内、1つはChangeメソッド内です.
例:リファレンスを使用してリファレンスタイプを渡す
この例では、メソッドヘッダと呼び出しでrefキーを使用する以外は、前の例と同じです.メソッド内で発生した変更は、呼び出しプログラムの元の変数に影響します.
C#
 
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]);

    }

}




しゅつりょく
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つの要素の配列を指します.
例:2つの文字列を交換する
交換文字列は、参照によって参照タイプパラメータを渡す良い例です.この例ではstr 1とstr 2の文字列をMainで初期化し、refキーによって変更されたパラメータとしてSwapStringsメソッドに渡す.この2つの文字列は、メソッド内およびMain内で交換されます.
C#
 
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);

    }

}




しゅつりょく
Inside Main, before swapping: John Smith
Inside the method: Smith John
Inside Main, after swapping: Smith John
コードディスカッション
この例では、呼び出しプログラムの変数に影響を与えるためにパラメータを参照して渡す必要があります.メソッドヘッダとメソッド呼び出しからrefキーを同時に削除すると、呼び出しプログラムに変更は発生しません.