When an object returns from a method, is a new instance or reference created?

Possible duplicate:
Are there any methods that return reference types, return links, or a cloned copy?

One of my colleagues stated that when a method returns an object like the following, a new instance / copy of the object is created, not a backlink:

public CustomerEntity Customer { get; set; } public CustomerEntity GetCustomer() { Customer = new CustomerEntity(); return Customer; } 

It is right? My tests seem to point to something else, but I'm not sure how to confirm this. He is concerned about the overhead of copying data to a new facility.

For a good measure, in which of the following methods / scripts are new objects created? In what situations does the calling class access the link or copy of the source object? Suppose CustomerEntity is a very large object.

 public class CustMan { public CustomerEntity GetCustomer() { Customer = new CustomerEntity(); return Customer } public void FillCustomer(CustomerEntity customer) { customer = new CustomerEntity(); // Calling class: // CustomerEntity ce = new CustomerEntity(); // l_custMan.FillCustomer(ce); WriteLine(ce.Name); } public void LoadCustomer() { Customer = new CustomerEntity(); // Calling Class access customerEntity via l_custMan.CustomerEntity } } 

Clarification: my colleague believes that it is better to use the Download method than the Get method:

 l_custMan.Load(); CustomerEntity = l_custMan.Customer; 

against.

 CustomerEntity = l_custMan.GetCustomer(); 
+6
source share
3 answers

One of my colleagues stated that when a method returns an object like the following, a new instance / copy of the object is created, not a backlink:

Your colleague is incorrect * . For return types that are reference types, a copy is always made, this is just a copy of the link that was made.

I can be more explicit:

Assuming ReturnType is a reference type and is given

 public ReturnType M() { // some code return E; } 

where E is the expression that evaluates to an instance of I in ReturnType , you ask if a copy of C was made from I and the reference to C returned to the caller, or if the reference to I returned to the caller. The answer is that the reference to I returned to the caller.

This is the same for the reference type parameters passed to the methods (if they are not marked with ref or out ): the copy is always executed, it is just a copy of the link that is passed.

* : in his defense, he is probably confused by some knowledge of C ++, where you should be explicit that you are returning a link.

+8
source

As long as only the link is returned, your method seems odd:

 public CustomerEntity GetCustomer() { Customer = new CustomerEntity(); return Customer; } 

The name assumes that it returns a field, instead it creates a new object, overwrites the property, and then returns a link, so each object is created each time.

Probably this method should be as follows:

 public CustomerEntity GetCustomer() { return Customer; } 

But this is superfluous, since your property already has a getter.

(I assume that CustomerEntity is a class, not a structure, a copy is created for the structures)

0
source

You will return a link to the object. To create a β€œnew” copy, you will need to create a completely new entity.

 public CustomerEntity GetCustomer() { CustomerEntity toReturn = new CustomerEntity(); return toReturn ; } 
-1
source

Source: https://habr.com/ru/post/894937/


All Articles