Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:57:55+00:00 2026-06-14T16:57:55+00:00

I’ve been struggling to understand what’s going on with a method I’ve got to

  • 0

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 ;]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:57:56+00:00Added an answer on June 14, 2026 at 4:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I have been unable to fix a problem with Java Unicode and encoding. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.