简体   繁体   中英

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. 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.

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.

Like @InBetween described:

Arguments are passed by copy by default in C#, but what is copied is the variable

That means, if you set the data in your method to null:

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

it would not set your dataToProcess to 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#. 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.

Arrays in c# can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.For further you should look out to this msdn doc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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