0

i am still struggling to understand basic. i've been ignoring this but now I need to fit this whole system in my brain .please see code below.if I set value of 'y'=x , then changing vale of 'y' won't change the value of 'x'. but if its an array , we go y=x, y[0]=foo then x[0] will be foo as well. I know toArray method will fix the issue and make a new copy and and won't change the original. how normal variables and arrays behave differently ?

 int m = 2;
 int n = m;
 n = 3;

Console.WriteLine("n = "+n);
Console.WriteLine("m = "+m);
Console.WriteLine();


int[] x = {1};
int[] y = x; 
y[0] +=1;

Console.WriteLine("x[0] = "+ x[0]);
Console.WriteLine("y[0] = "+ y[0]);

Console.WriteLine();

Console.WriteLine(x==y);
Console.WriteLine(n == m);

Output:

n = 3 
m = 2

x[0] = 2 
y[0] = 2

True
False
Yugraaj Sandhu
  • 392
  • 3
  • 14
  • 5
    It's not about arrays - it's about reference types and value types. See https://www.jonskeet.uk/csharp/references.html – Jon Skeet Apr 11 '23 at 09:27

2 Answers2

1

it is not about web programming ... Its about strategy ...

Imagine you want to organize a video sharing website like YouTube and for example someone wants to share a video clip ...

There are two ways ...

  1. You save your video somewhere and every time you want to share it with someone, send the address to that person. In this case, you can change the content of the video by changing the content in that address. Also, any person who has that address and uploads a new video at that address. The video you posted will be changed.

  2. The second method is to store the entire video content somewhere and send the entire video to someone every time they request it, instead of just sending them the address.

It is clear that the first method has more advantages. The point here is that:

In the first method, you are working with addresses. And in the second method you are working with values.

Therefore, the events that happen are different.

In the first example, you have two variables, both of which are content type (not address type) and both of which have separate addresses in memory. You set one equal to 2 and then you set the contents of the second house equal to the contents of the first house. Therefore, both get a value of 2. Now you set the value of the second house equal to 3, why this issue should affect the content of the first house.

In the second example, you have two variables (Address type) that both point to the same place. For example, you have two variables, both of which contain a link from YouTube (both links point to a video and an address).

Now if the content inside that address changes. For example, change the video where the address is located. Both links see this change.

0

int[] is a reference type while int is a value type. When assigning n = m a new variable is created (stored in the memory), with the value of m. However, when assigning y = x, y is just a reference to where x is stored in the memory. that's why changing y also changes x (you change the value of the "cell" in memory they are stored since both x and y points to the same "cell" in memory).

Lior v
  • 464
  • 3
  • 12