简体   繁体   English

将数组作为参数传递

[英]Passing arrays as parameter

If we modify the content of the array passed as parameter inside a method the modification is done on the copy of the argument instead of the original argument hence the result is not visible. 如果我们修改在方法中作为参数传递的数组的内容,则对参数的副本而不是原始参数进行修改,因此结果不可见。

What is the process that happens when we call method that have reference type argument? 当我们调用具有引用类型参数的方法时,会发生什么过程?

Here is the code sample of what I want to ask 这是我想问的代码示例

      using System;

namespace Value_Refrence_Type
{
    class Program
    {
        public static void Main()
        {
            int[] callingarray = { 22, 200, 25485 };
            abc(callingarray);
            Console.WriteLine("This is callingarray");
            foreach (int element in callingarray)
                Console.WriteLine(element);
        }



        //method parameter
        static void abc(int[] calledarray)
        {
            Console.WriteLine("Method Called--------");
            foreach (int element in calledarray)
                Console.WriteLine(element);

            //Here on changing the value of elements of calledarray does't afftect the value of element of callingarray
            //if both refrences to same memory location then the value needs to change, which is not happening here
            calledarray = new int[] {55, 54, 65};
            foreach (int element in calledarray)
                Console.WriteLine(element);
        }

    }
}

No, that is not correct. 不,这不正确。

Arguments are passed by value by default in C#, which means you get a copy of the variable. 默认情况下,参数在C#中按值传递,这意味着您将获得变量的副本。 But it's important to realize that what is copied is just the variable, not necessarily the object; 但重要的是要意识到复制的只是变量,不一定是对象; if the variable holds a reference type (an array for example) then the variable is really just a "pointer" to a memory address where the object lives. 如果变量包含引用类型(例如数组),则变量实际上只是对象所在的内存地址的“指针”。 So when you pass said variable to a method call, the reference is copied yes, but it still points to the exact same object the original variable refers to. 因此,当您将所述变量传递给方法调用时,引用将被复制为yes,但它仍然指向原始变量引用的完全相同的对象。

Things are very different when the argument is a value type. 当参数是值类型时,情况就大不相同了。 In that case the variable itself holds the object and therefore you'd get the behavior you seem to be expecting. 在这种情况下,变量本身保存对象,因此您将获得您似乎期望的行为。

Here are many answers with a description. 这里有许多答案和描述。 I try to give an example. 我试着举个例子。 Let's say this is your method: 让我们说这是你的方法:

public void ProcessData(int[] data)
{
  data[0] = 999;
}

If you call the method like this: 如果你调用这样的方法:

int[] dataToProcess = new int[] {1, 2, 3};
ProcessData(dataToProcess);
Console.WriteLine(dataToProcess[0]); //returns 999;

it will return 999 because ProcessData accesses the memory of the array. 它将返回999,因为ProcessData访问数组的内存。

Like @InBetween described: 像@InBetween描述的那样:

Arguments are passed by copy by default in C#, but what is copied is the variable 默认情况下,在C#中通过副本传递参数,但复制的是变量

That means, if you set the data in your method to null: 这意味着,如果将方法中的数据设置为null:

public void ProcessData(int[] data)
{
  data = null;
}

it would not set your dataToProcess to null. 它不会将dataToProcess设置为null。 This means: 这意味着:

You are passing the copy of a pointer to the memory of your array to the method. 您将指向数组内存的指针的副本传递给方法。

Arrays are a reference type in C#. 数组是C#中的引用类型。 This means that each time an array is passed as an argument to any function, the reference (or pointer) to the argument is passed to the function. 这意味着每次将数组作为参数传递给任何函数时,参数的引用(或指针)都会传递给函数。 Thus any modifications you make to the array in the function are made in the actual argument also. 因此,您对函数中的数组所做的任何修改也都是在实际参数中进行的。

If we modify the content of the array passed as parameter inside a method the modification is done on the copy of the argument instead of the original argument hence the result is not visible. 如果我们修改在方法中作为参数传递的数组的内容,则对参数的副本而不是原始参数进行修改,因此结果不可见。

The behavior you are referring to here is for value types and not reference types. 您在此处引用的行为是类型而不是引用类型。 Value types in C# are struct s and enum s, and reference types in C# are class es and arrays. C#中的值类型是structenum ,C#中的引用类型是class es和数组。

Arrays in c# can be passed as arguments to method parameters. c#中的数组可以作为方法参数的参数传递。 Because arrays are reference types, the method can change the value of the elements.For further you should look out to this msdn doc. 因为数组是引用类型,所以该方法可以更改元素的值。为了进一步,您应该注意这个 msdn doc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM