So first off, there are a ton of differences between Javascript and C#. I'm not going into details here, it would simply take too long.
A few things that are noteworthy:
You can pass the array by reference, that'll solve the immediate problem. You should however learn what 'by reference' means before you try that.
public static void update(ref string [] array)
{
string [] newArray = new string [2];
newArray[0] = "myself";
newArray[1] = "version2";
array = newArray;
}
Also:
Arrays in Javascript are more like 'Dictionary' objects in C#. Arrays in C# are basically just a chunk of sequential memory. It's quite different, so I think it's a good idea to learn that difference.
Scope is very different. You might want to learn everything you can get your hands on about scope in C#. That's basically the issue you're having here as well. Once you understand that, you probably also won't pass it by ref anymore.
It's funny, some people here have approached this question from a C# perspective while others have approached it from JavaScript.
To complete the definitions:
- Pass by value means that you take the bytes of the thing, and pass it to a function, whatever that 'thing' may be.
- Pass by reference means that you take the memory address of the bytes of the thing, and pass that to a function.
If you call a function, assume that the things you pass to the function are copied. In other words, you either copy the address of the value (pass by reference) or the value itself (pass by value).
First off, how it works in Javascript:
As @T.J.Crowder points out, technically, all passes in Javascript are by-value, although I prefer the term 'call by sharing'. A value here can be a pointer to an object, and if the object supports it, you can change the object. An array is an object that allows you to change the contents of the array.
There are a few good examples on how this works exactly here: Javascript by-reference or by-value
Next, how it works in C#
In C#, you can pass objects by value or by reference. If you were to pass a Dictionary (which is IMHO the best translation for an array in Javascript), you can simply modify the contents:
public static void update(Dictionary<int, string> array)
{
array[0] = "myself";
array[1] = "version2";
}
The reason this works is because a Dictionary
is a reference type, which means the pointer to the array is passed to the update
function.
However, because the pointer to the array is passed 'by-value' to the update
method, this also means the pointer itself is immutable from the caller's perspective. You can attempt to overwrite 'array' in the update method, but updates will be lost as soon as the update
method exists because the pointer that was passed is not changed in the method that's calling update
.
Confusing? Let's make it simple:
public static void update(Dictionary<int, string> array)
{
Debugger.Break(); // look at the contents of array here
array = new Dictionary<int, string>();
array[0] = "myself";
array[1] = "version2";
Debugger.Break(); // look at the contents of array here
}
public static void Main()
{
var foo = Dictionary<int, string>();
foo[0] = "aap";
update(foo);
Debugger.Break(); // look at the contents of foo here
}
If you want to overwrite the pointer in the caller method, you need to pass it 'by reference', which basically means you pass the address to the pointer. The compiler automatically dereferences the address and updates the underlying value (the 'real' object reference) if you assign it:
public static void update(ref Dictionary<int, string> array)
{
auto myArray2 = new Dictionary<int, string>();
myArray2[0] = "myself";
myArray2[1] = "version2";
array = myArray2;
}