Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# confusing method

Tags:

arrays

c#

testing

I've been struggling to understand what's going on with a method I've got to understand for my test, I'm having a hard time figuring out the reason I'm getting the results I get, any explanation on how the "f" method works it's greatly appreciated

class Program {
  static void Main(string[] args)
  {
    A b = new A(); b.y = b.x;
    b.f(b.y); b.g();
    Console.WriteLine(b.x[0] + " " + b.x[1]); // Prints 1 7
    Console.WriteLine(b.y[0] + " " + (b.x[1] + b.y[1])); // 1 14
  }
}

public class A {
  public int[] x = {1, 2};
  public int[] y;
  public void f(int[] z)
  {
      z[1] += 5;
  }

  public void g()
  {
      A a = new A ();
      a.x[0]++;
      a.x[1]--;
  }
}

Let me explain what I did understand, b.y gets created as an array and it gets the values in b.x, now, when we call b.f, we pass that method b.y which is [1, 2], now, and here's where I get stuck, z seems to be the b.y array, so it has [1, 2] as value, when the method adds 5 to the element in the position 1 (which is 2) I get [1, 7] as result of that, when the method ends and my program goes back to the main, somehow, b.y AND b.x BOTH are now [1, 7], how did that happen?, I thought the method was only modifying b.y since that's the one that got passed. Also, the g function doesn't add anything as the "a" value is a local variable that "dies" as the method ends, right?. I hope someone can help me, I've got to pass this test!. Thanks ;]

like image 507
Kurt H Dix Avatar asked Oct 06 '22 22:10

Kurt H Dix


2 Answers

Here we go:

  • A new variable named b is initialized with a type of A.
  • When b is created, it sets the value of b.x to {1, 2}.
  • b.y is then assigned to b.x, however, because they are arrays, they are now referencing the same data.
  • b.f is called, and has b.y passed to it (remembering, that b.y and b.x are referencing the same data right now). Essentially, z also points to the same data during the f function.
  • b.f adds 5 to the value at index 1 of the shared data, which is 2. So 2 + 5 = 7.
  • First Console.WriteLine prints b.x[0] which is still 1. Then it prints b.x[1] which is now 7 (as above).
  • Second Console.WriteLine prints b.y[0], which is still 1 (because they share the same data). Then it prints b.x[1] + b.y[1]. They both share the same data.. and the data at index 1 is 7. 7 + 7 = 14.

You are correct about the g method in that the variable is local and doesn't do anything.

Hope that helps.

like image 79
Simon Whitehead Avatar answered Oct 13 '22 12:10

Simon Whitehead


b.y = b.x;

This line of code only copies a reference kept in x variable to y variable. So both variables reference one and the same array. So, you have only one array and two variables refering to it.

public void g(){...}

This method creates a new element, does something with it but doesn't store it anywhere, so it's lost (there are no references on it, it's ready to be garbage collected) after the method returns.

enter image description here

Try read Value vs Reference

like image 32
horgh Avatar answered Oct 13 '22 10:10

horgh