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 8515195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:04:56+00:00 2026-06-11T05:04:56+00:00

I’ve come across this tricky issue. See the following code: public class Foo public

  • 0

I’ve come across this tricky issue. See the following code:

public class Foo
    public int A { get; set; }
}

static void Main(string[] args)
{
    Foo foo1 = new Foo();
    Foo foo2 = new Foo();

    ChangeObject1(foo1);
    ChangeObject2(ref foo2);

    Console.WriteLine("Foo1 = " + foo1.A);
    Console.WriteLine("Foo2 = " + foo2.A);
    Console.ReadKey();
}

static void ChangeObject1(Foo foo)
{
    foo.A += 1;
    foo = new Foo();
}

static void ChangeObject2(ref Foo foo)
{
    foo.A += 1;
    foo = new Foo();
}

After some testing, foo1.A is equal to 1, and foo2.A is still equal to 0. To my understanding, this is because the second method has the instance passed in by reference rather than value. This changes the content of the instance, so when a new object is created and assigned in the method, the value is no longer defined.

But thinking about it confuses me as I feel that there should be similar effects in the first method, even if it’s passed by value because the content of the instance is still changed outside of the method.

Anyone know whats actually going on? 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-11T05:04:57+00:00Added an answer on June 11, 2026 at 5:04 am

    I presume that you’re using c# or java. either way, the concept remains the same

    when you declare your foo variables

    Foo foo1 = new Foo()
    

    you’re declaring what is called a reference type. (all objects that represent classes are reference types)

    so, the value stored in foo1 isn’t really a Foo, but it’s a value that holds the memory address of that Foo.

    when you call

        static void ChangeObject1(Foo foo)
        {
            foo.A += 1;
            foo = new Foo();
        }
    

    you’re passing a copy of that reference. Therefore, when you do foo.A += 1, you’re changing the member value of the object that you’ve just passed in.

    when you do foo = new Foo(); you’re only changing the copy of the reference, but not the original object reference itself.

    now, when you call

        static void ChangeObject2(ref Foo foo)
        {
            foo.A += 1;
            foo = new Foo();
        }
    

    you’re not passing a copy of the address of your Foo but you’re passing the address of the address of your Foo.

    you can think of it as though you’re passing the original Foo address itself.

    So when you call foo = new Foo(), it doesn’t matter what you did to foo before that. You’ve just over-written your address with the address of a brand new Foo object.

    if you change your method to look like the following

        static void ChangeObject2(ref Foo foo)
        {
            foo.A += 1;
        }
    

    then you should notice the change

    Here’s a page that explains reference types It’s in java, but the same principles apply to c# too

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.