Search code examples
c#visual-studiomultiple-instancesdefault-copy-constructor

C# Instance copy / Passing object reference is different then Java?


class Player
{
   private Location location;

   public Location getLocation()
   {
        return location;
   }

    public void setLocation(Location location)
    {
        this.location = location;
    }
}

...

class Location
{
    int x,y,z;

    public Location(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public Location(Location location)
    {
        this.x = location.x;
        this.y = location.y;
        this.z = location.z;
    }


    public void updateLocation(Location location) //the fix..
    {
        this.x = location.x;
        this.y = location.y;
        this.z = location.z;
    }
}

Say.. you do

Player p1 = new Player();
Player p2 = new Player();
p1.setLocation(p2.getLocation());

Now the bug/problem occurs when you attempt to modify other persons location. Both players locations changes exactly the same, as they both now share the same location.

So of course this below will work just fine.

p1.setLocation(new Location(p2.getLocation()));

but the problem is it always creates a new object.. when I could just update the existing instance..? how do I update the existing instance on default without making my own methods like I did below to fix this.

I had to fix this using the method below (any way to do this by default without doing like this below)

    public void setLocation(Location location)
    {
        if (this.location == null)
            this.location= new Location(location);
        else
            this.location.updateLocation(location);
    }

Anyone knows any tricks I may not know about? Thanks.


Solution

  • Classes are reference types. When you call this:

    p1.setLocation(p2.getLocation());
    

    you are not passing instance data to the method but the reference to the instance. Reference is like managed pointer to heap (memory) where the instance's data are stored. That is quite important because otherwise all changes to instance's data would have only method scope. When you call this:

    this.location = location;
    

    you are assigning the reference not the data. It means that both location and this.location will point to the same block in the memory.

    That is the essential point in C# programming. If you want data to be copied instead of referenced you must use structures instead of classes.

    Anyway reassiging values from one instance to another instance is quite common task.

    Edit:

    You pointed that you don't want to create new object because of overhead.

    • Premature optimization is source of evil
    • Optimize where it make sense and where you see that it really make performance drop. Here you have some values about object instantiation on simple laptops.

    Also saying that you don't like properties is strange - you are programming in C# so use it correctly otherwise your colleagues will not like you.