C # instance instance / pass object reference different from 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 an error / problem occurs when you try to change the location of others. Both player locations change in the same way, as they both now have the same location.

So, of course, this will work very well.

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

but the problem is that it always creates a new object .. when could I just update an existing instance ..? how can I update an existing default instance without doing my own methods, as I did below to fix this.

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

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

Does anyone know any tricks that I may not know about? Thanks.

+4
source share
2 answers

Classes are reference types. When you call this:

 p1.setLocation(p2.getLocation()); 

you do not pass instance data to the method, but a link to the instance. A link is like a managed pointer to a heap (memory) where instance data is stored. This is very important, because otherwise all changes to the instance data will only have the method scope. When you call this:

 this.location = location; 

You are assigning a link to data that is not. This means that both location and this.location will point to the same block in memory.

This is an important point in C # programming. If you want to copy data instead of a link, you must use structures instead of classes.

In any case, redefining values ​​from one instance to another instance is a fairly common task.

Edit:

You indicated that you do not want to create a new object due to overhead.

  • Premature Optimization - The Source of Evil
  • Optimize where it makes sense and where you see that it really reduces performance. Here you have some implications about creating objects on simple laptops.

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

+7
source

Passing links in .NET is essentially the same as Java. However, .NET has several additional features.

For example, the out and ref parameters can be used to return the values ​​passed as arguments. eg

  public void SaveProductFeature(ref SaveCollection save) { SaveCollection.Product.ProductId = Product.Save(SaveCollection.Product); } 
0
source

All Articles