+ 4

How is the ouput of this x[0] = 4?

This is a quiz in the C# section. I really dont get how the output here is 4. Completely debugged it with VS17 and still dont get why the line: x[0] = 4; changes the array, but the line: a = new int[3] {1,2,3}; doesn't. Can someone explain this? static void Main(string[] args) { int[] x = new int[3]{5, 6, 7}; func(x); Console.WriteLine(x[0]); } static void func(int[] a) { a[0] = 4; a = new int[3] { 1, 2, 3 }; } Edit: I thought the output was 1, not 4.

14th Jun 2018, 11:26 AM
Kevin Kalb
Kevin Kalb - avatar
11 odpowiedzi
+ 9
Well this might be hard to grasp. There are 2 types of variables called value type and reference types (http://net-informations.com/faq/general/valuetype-referencetype.htm). The important point to note about them is that value types are stored directly on stack while reference types are stored on heap memory and their REFERENCE is stored on stack. This is the reason why when you pass a variable int a to a method and modify it whatever inside the method it won't affect the original int a in the calling function where as arrays are reference types (infact everything you initialize with a "new" keyword are reference type) so when you pass a array to a method you're not passing the arrays value but the arrays memory address so what every change you make to it are reflected back in calling function. But when you use the "new" keyword the reference address is overwritten and a new heap memory is allocated hence any changes to the new location in heap doesn't reflect back in the original array.
14th Jun 2018, 1:27 PM
Rusty.Metal
+ 4
All reference types works that way if they're defined inside methods or not Kevin Kalb.
14th Jun 2018, 2:47 PM
Rusty.Metal
+ 3
That made perfect sense, rusty metal. Thanks!
14th Jun 2018, 1:33 PM
Just A Rather Ridiculously Long Username
+ 3
Kevin Kalb you probably will never need to use codes like these in real life program but it's just a concept you might need to know.
14th Jun 2018, 2:09 PM
Rusty.Metal
14th Jun 2018, 1:34 PM
Rusty.Metal
+ 1
I think with a[0] = 4 it changes the first element of the array, so the array is 4, 6, 7. After that print the result x[0], which now is 4 and nesting in the first array second array, so it is now a matrix.
14th Jun 2018, 12:19 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Take this with a grain of salt, (I don't know C#) but saying a = new int[3] { 1, 2, 3 }; should replace its original value with a new array right? How is there a possibility of a nested array?
14th Jun 2018, 12:43 PM
Just A Rather Ridiculously Long Username
+ 1
No, it's because you are calling function (x) and then static void func (int [] a) your argument x is array int[] a. After that declaring a is also array, so it becomes array in array - a matrix.
14th Jun 2018, 12:55 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Okay, so you're saying that the new array [1, 2, 3] is nested within the original array? In that case, would x look like this? [4, [1, 2, 3]] Also, how would you properly replace the value of x?
14th Jun 2018, 1:05 PM
Just A Rather Ridiculously Long Username
0
гยรtץ ๓єtคl thanks mate, that seems like the point i was looking for. I wrote quite a few programs in c# and i didnt encountered anything like this at all. Marked as the answer.
14th Jun 2018, 2:08 PM
Kevin Kalb
Kevin Kalb - avatar
0
So is it generally call by reference when i use an array as a parameter when calling methods? Because when you declare the array outside of the main() it works as expected (all call by value).
14th Jun 2018, 2:14 PM
Kevin Kalb
Kevin Kalb - avatar