Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }

    }
}
like image 298
mdadil2019 Avatar asked May 28 '16 11:05

mdadil2019


People also ask

Can we pass array as a parameter of function?

Just like normal variables, simple arrays can also be passed to a function as an argument, but in C/C++ whenever we pass an array as a function argument then it is always treated as a pointer by a function.

Can you pass arrays as parameters in C++?

In C++, we can pass arrays as an argument to a function.

When an array is passed as parameter to a function?

Explanation: When an array is passed as parameter to a function then the function can change values in the original array.

How do you pass an array to a parameter in C#?

In C#, arrays are the reference types so it can be passed as arguments to the method. A method can modify the value of the elements of the array. Both single-dimensional and multidimensional arrays can be passed as an argument to the methods.


4 Answers

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.

like image 196
InBetween Avatar answered Nov 15 '22 12:11

InBetween


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.

like image 28
Fruchtzwerg Avatar answered Nov 15 '22 13:11

Fruchtzwerg


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 structs and enums, and reference types in C# are classes and arrays.

like image 45
EvilTak Avatar answered Nov 15 '22 13:11

EvilTak


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.

like image 35
Himanshu Dwivedi Avatar answered Nov 15 '22 13:11

Himanshu Dwivedi