-2

So I'm playing around with C# right now, coming from the world of C++.

I'm trying to make two object variables point to the SAME memory address, in a way that if I edit variable 1, variable 2 is also being changed.

Is this possible?

Reason I'm asking is because I would ideally like to have an instance of an object inside of one object, and then also inside of a different type of object. So that if the value is changed in one object, it changes in both. Like this (or at least something like this).

public class Demo
{
    public int x;
}

public class DemoContainer1
{
    public DemoContainer1(ref Demo new_demo)
    {
        demo = new_demo;
    }

    public int SetX(int x)
    {
        demo.x = x;
    }

    private Demo demo;
}

public class DemoContainer2
{
    public DemoContainer2(ref Demo new_demo)
    {
        demo = new_demo;
    }

    private Demo demo;
}

Demo new_demo = new Demo();
new_demo.x = 5;

DemoContainer1 container1 = new DemoContainer1(ref new_demo);
DemoContainer2 container2 = new DemoContainer2(ref new_demo);

container1.SetX(10);

// The following should now be true... container1.demo.x == 10 AND container2.demo.x == 10
15
  • 8
    This is the standard behavior of reference types. Commented Oct 5, 2016 at 19:50
  • 2
    Did you try it at all?
    – sstan
    Commented Oct 5, 2016 at 19:54
  • 2
    Did you run that code? They will both be 10.
    – TyCobb
    Commented Oct 5, 2016 at 20:07
  • 2
    We can't help you without seeing what you actually have or code that reproduces your issue. The code you provided works with what you wanted to achieve. You don't even need to pass them in as ref to achieve what you outlined in your code.
    – TyCobb
    Commented Oct 5, 2016 at 20:10
  • 1
    @PeterMoore Not even running your own code to see that it already does exactly what you want isn't particularly understandable. There's also tons of information on this subject out there, so there's very clearly not enough research done here.
    – Servy
    Commented Oct 5, 2016 at 20:57

1 Answer 1

10

Rick - If you're coming from the world of C++, the most important thing to remember is that any time you have an instance of a class or an interface in your code, this is roughly equivalent to a pointer in C++. We call these reference types. When you assign one class reference to another class reference variable, you are essentially doing exactly what you're asking to do, which is to have both variables point to the same object in memory.

Note there are also value types - these are the fundamental types (int, char, etc.) and structs. For struct's in particular, these behave very similarly to structs in C++, (although it's not particularly easy to obtain the equivalent of a pointer to a struct).

It can be confusing because the . operator in C# doubles as the member accessor for both reference and value types. (There is no separate -> operator like in C++). You just need to be aware of whether your data items are reference types or value types to determine whether an assignment (=) will result in the value being copied or the "pointer" (reference) being copied. Consequently, you cannot create copies of class instances by simply using the assignment operator = like you can in C++. For that you have to use the Object.MemberwiseClone method.

As observed in the comments, you don't need the ref in your situation because your method argument is already a "pointer". (The ref keyword is similar to the reference type flag (&) in C++). However, if you wanted to change the value of the input parameter new_demo itself, to assign it to a different class instance, then you would indeed need the ref.

So, if it helps as you transition, you might want to just mentally imagine the C/C++ pointer flag * after every reference type variable you define and mentally picture the -> when you see the . when you're accessing class members.

Hope this helps. As someone who also transitioned from C++ to C# I definitely can see how you would find it confusing at first. But in time you'll learn to speak both languages fluently.

Edit - I'd be curious to see your code that you say is not resulting in the right outcome. As has been pointed out, what you posted should work as written.

1
  • 1
    Absolutely great answer. I ended up finding the cause of problem (it was due to a copy constructor that I made and forget that was being used). This clears up my questions/vague knowledge of C# so far ;). Thank you.
    – Ricky
    Commented Oct 6, 2016 at 13:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.