0

I remember when talking function pass parameters, it is said the parameter will NOT get its value changed during function execution. but in my example, I observed my initial parameter value changed:

public static void main()
{
    int[] a = new int[]{some int array};
    int[] b = SortArray(a);
    Output(a);
}

internal static int[] SortArray(int[] a)
{
    int[] c = new int[a.length];
    //Sort process, during which, both c[] and a[] are changing
    return c;
}

when executing Output(a); I found my a is not the original one anymore. what's the trick here?

a4194304
  • 366
  • 2
  • 13
  • 3
    You'll need to share the code of `SortArray` in order to get a sensible answer – Mureinik Sep 11 '18 at 06:23
  • 1
    parameters are not immutable in c#. depends on what you're doing with the a array, it could change – Thomas Sep 11 '18 at 06:25
  • 1
    Possible duplicate of [Is int\[\] a reference type or a value type?](https://stackoverflow.com/questions/1533757/is-int-a-reference-type-or-a-value-type) – Waayd Sep 11 '18 at 06:30
  • 1
    Because all array in c# are referance type, so if you pass an array to a method and do something like update or delete value, the values of array will completely change . – Waayd Sep 11 '18 at 06:33

3 Answers3

6

it is said the parameter will NOT get its value changed

That is right*. The confusion you have here might be because you misunderstood what "change" here means.

Arrays in C# are reference types. This means that variables of array types do not store the arrays themselves, but rather a reference to the actual array, which is somewhere else in memory.

a's value is not the array, it is the reference. As long as a is referring to the same array, I can say that a's value is not changed.

In your method, you probably did something like:

a[0] = <something else>;

This is not modifying the value of a. You are modifying the actual array object. Modifying the value of a would be something like:

a = new int[10]; // a now references a new array

So that's why the changes reflect - there is only one array to start with, both the parameter a and the local variable a refer to that.


*Actually, if you use the ref modifier, the parameter's value can be changed because it is passed by reference.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Arrays in .NET are an object on the heap, so you have a reference.

That reference is passed by value, ergo changes to the contents of the array will be seen by the caller, yet reassignment of the array wont

//Sort process, during which, both c[] and a[] are changing

Don't change a[x], problem sorted.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

Should be like this. You should avoid returning array, just pass it as reference / pointer. Also forget all about java array declaration (order of brackets)

public static void main()
{
    int a[] = {3,1,2};
    int* sorted = SortArray(a, a.length);
    Output(sorted);
    delete[] sorted;
}

internal static int* SortArray(int* a, int length)
{
    int* c = new int[length];
    //Sort process, during which, both c[] and a[] are changing
    return c;
}
Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16