通过值和通过引用传递数组 [英] Passing Arrays by Value and by Reference

查看:145
本文介绍了通过值和通过引用传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些例子从我读只是有一个小麻烦抓住了这个例子实际上做想解释,以帮助我进一步了解这里发生了什么一个C#的书。

These are example from a c# book that I am reading just having a little trouble grasping what this example is actually doing would like an explanation to help me further understand what is happening here.

        //creates and initialzes firstArray
        int[] firstArray = { 1, 2, 3 };

        //Copy the reference in variable firstArray and assign it to firstarraycopy
        int[] firstArrayCopy = firstArray;

        Console.WriteLine("Test passing firstArray reference by value");


        Console.Write("\nContents of firstArray " +
            "Before calling FirstDouble:\n\t");

        //display contents of firstArray with forloop using counter
        for (int i = 0; i < firstArray.Length; i++)
            Console.Write("{0} ", firstArray[i]);

        //pass variable firstArray by value to FirstDouble
        FirstDouble(firstArray);

        Console.Write("\n\nContents of firstArray after " +
            "calling FirstDouble\n\t");

        //display contents of firstArray
        for (int i = 0; i < firstArray.Length; i++)
            Console.Write("{0} ", firstArray[i]); 

        // test whether reference was changed by FirstDouble
        if (firstArray == firstArrayCopy)
            Console.WriteLine(
                "\n\nThe references refer to the same array");
        else
            Console.WriteLine(
                "\n\nThe references refer to different arrays");

       //method firstdouble with a parameter array
       public static void FirstDouble(int[] array)
    {
        //double each elements value
        for (int i = 0; i < array.Length; i++)
            array[i] *= 2;

        //create new object and assign its reference to array
        array = new int[] { 11, 12, 13 };

基本上有是code我想知道的是,如果阵列是按值传递的书是说比原来的调用者没有得到该方法(从我的理解)的改进型。所以,对方法的最后FirstDouble他们尝试将本地变量数组到一组新的元素而失败,并在原主叫方的新值的显示时是2,4,6。

Basically there is the code what I would like to know is that the book is saying if the array is passed by value than the original caller does not get modified by the method(from what i understand). So towards the end of method FirstDouble they try and assign local variable array to a new set of elements which fails and the new values of the original caller when displayed are 2,4,6.

现在我的困惑是怎么的在方法循环FirstDouble修改原来的调用者firstArray为2,4,6,如果它是按值传递。我认为值应保持1,2,3。

Now my confusion is how did the for loop in method FirstDouble modify the original caller firstArray to 2,4,6 if it was passed by value. I thought the value should remain 1,2,3.

在此先感谢

推荐答案

理解的关键,这是要知道值之间的差值类型和引用类型

The key to understanding this is to know the difference between a value type and a reference type.

例如,考虑一个典型的价值型, INT

For example, consider a typical value type, int.

int a = 1;
int b = a;
a++;

在此code已经执行, A 的值为2,和 B 值为 1 。因为 INT 是值类型, B = A 需要的值的副本

After this code has executed, a has the value 2, and b has the value 1. Because int is a value type, b = a takes a copy of the value of a.

现在考虑一个类:

MyClass a = new MyClass();
a.MyProperty = 1;
MyClass b = a;
a.MyProperty = 2;

由于类是引用类型, B = A 只是分配的参考,而不是价值。因此, B A 指的是同一个对象。因此,在 a.MyProperty = 2 执行 b.MyProperty == 2 ,因为 A b 引用同一个对象。

Because classes are reference types, b = a merely assigns the reference rather than the value. So b and a both refer to the same object. Hence, after a.MyProperty = 2 executes, b.MyProperty == 2 since a and b refer to the same object.

在考虑你的问题code,数组是引用类型,因此此功能:

Considering the code in your question, an array is a reference type and so for this function:

public static void FirstDouble(int[] array)

变量阵列实际上是一个参考,因为 INT [] 是引用类型。因此,阵列引用的是按值传递

the variable array is actually a reference, because int[] is a reference type. So array is a reference that is passed by value.

因此​​,修改阵列所做的功能实际上是应用于 INT [] 对象,该<内部code>阵列指。因此这些修改都引用该同一对象的所有引用可见。而且,其包括呼叫者持有参考

Thus, modifications made to array inside the function are actually applied to the int[] object to which array refers. And so those modifications are visible to all references that refer to that same object. And that includes the reference that the caller holds.

现在,如果我们看一下这个函数的实现:

Now, if we look at the implementation of this function:

public static void FirstDouble(int[] array)
{
    //double each elements value
    for (int i = 0; i < array.Length; i++)
        array[i] *= 2;

    //create new object and assign its reference to array
    array = new int[] { 11, 12, 13 };
}

有一个进一步复杂化。在循环简单加倍的每个元素 INT [] 传递给函数。这是调用者看到的修改。第二部分是一个新的 INT [] 对象的局部变量阵列的分配。因为它所做的是改变参考阵列的目标,这是不是给调用者可见。而且,由于参考阵列是按值传递,调用者没有看到新的对象。

there is one further complication. The for loop simply doubles each element of the int[] that is passed to the function. That's the modification that the caller sees. The second part is the assignment of a new int[] object to the local variable array. This is not visible to the caller because all it does is to change the target of the reference array. And since the reference array is passed by value, the caller does not see that new object.

如果该功能已经声明如下:

If the function had been declared like this:

public static void FirstDouble(ref int[] array)

则引用阵列将已按引用传递和调用者会看到新创建的对象 {11,12,13} 当函数返回。

then the reference array would have been passed by reference and the caller would see the newly created object { 11, 12, 13 } when the function returned.

这篇关于通过值和通过引用传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆