Does copying reduce performance?

First of all, I don’t know if this is called “copying” an object, so the question title may be incorrect. I have a custom class called "File" that contains a bunch of lists (1000+ numbers in 5-6 lists) and some other properties. I have all my files on the list to have access to (LoadedFiles).

My question is: if I write a method and I do not want to write:

DoSomeOperation(LoadedFiles[2]); 

but instead:

 File file2 = new File(); file2 = LoadedFiles[2]; DoSomeOperation(file2); 

Is this a bad practice? Or the compiler is smart enough to know that it is the same object and access it directly from the source list (LoadedFiles).

+4
source share
5 answers

Actually, your class is a reference type, which means that the line file2 = LoadedFiles [2] will only copy the reference pointer to the object that was created, you will not copy the contents of the object to the new object.

So, as far as performance is concerned, you create a new file instance:

  File file2 = new File(); 

Then you immediately switch the link to another object

 file2 = LoadedFiles[2]; 

Thus releasing the reference to the object you just created. This will cause unnecessary garbage collection. It’s better to just do File file2 = LoadedFiles [2] if that matters stylistically to you.

The best place to look for reference types and value types is the C # language specification http://www.microsoft.com/en-us/download/details.aspx?id=7029 on page 77.

+6
source

IN

 File file2 = new File(); file2 = LoadedFiles[2]; 

copying doesn't matter. Only the link is copied. But an unnecessary instance of File() . Drain it:

 File file2 = LoadedFiles[2]; 
+3
source

This is not a bad practice. And you do not copy the object, you just keep a reference to it (pointer 4 bytes).

This is often due to the coding style. I personally do not like the first method, since it complicates the reading and often I need to check the return value of the function, therefore

 va file2 = LoadedFiles[2]; 

- This is just a convenient choice for me.

Also it makes no sense to write

 File file2 = new File(); file2 = LoadedFiles[2]; 

just write:

 var file2 = LoadedFiles[2]; 
+1
source

First, I hope that you assign your class name to the correct namespace, because I anticipate a conflict with System.IO.File .

Secondly, If this statement is file2 = LoadedFiles[2]; is legal and returns an instance of your File class, then File file2 = new File(); has no purpose.

You pass by reference an instance of your File class to DoSomeOperation without making a copy of it.

+1
source

Let's start with the code:

 File file2 = new File(); file2 = LoadedFiles[2]; 

The first line is redundant. You select a new file, just to say “hey look, forget about it, take that one from the array”. Well, do not create a new file, and then:

 File file2 = LoadedFiles[2]; 

Now it looks like you copied everything that is in LoadFiles [2]. But you didn’t. All that you copied was a reference to this object. There is still only one object, and now two variables know about it.

For comparison: Your friend lives on Oak Street, 123. You have this address in your address book. This is a link. You don’t have his real house, just a link to his house so you can find him. When you copy this link by sending it to another friends phone book, the link has been copied. The original house is still the only house. After copying the address, there was no second home.

0
source

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