Reputation: 4125
First of all, I don't know if this is called "copying" an object, so the question title may be wrong. What I have is a custom class called File which has inside a bunch of lists (1000+ numbers in 5-6 lists) and some other properties. I have all my files in a List to be able to access them (LoadedFiles).
My question is: if I'm writing a method and I don't 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 clever enough to know that it's the same object and access it directly from the original list (LoadedFiles).
Upvotes: 4
Views: 99
Reputation: 4221
Firstly - I hope you are qualifing your class name with the correct namespace because I foresee a conflict with System.IO.File
.
Secondly;
If this statement file2 = LoadedFiles[2];
is legal and returns an instance of your File class, then the File file2 = new File();
serves no purpose.
You are passing by reference an instance of your File class to DoSomeOperation, not making a copy of it.
Upvotes: 1
Reputation: 4675
Actually your class is a reference type which means that the line file2 = LoadedFiles[2] will only copy a reference pointer to the object that was created, you're not copying the contents of the object into a new object.
So as far as performance is concerned you a creating a new instance of a file:
File file2 = new File();
Then you're immediately switching the reference to a different object
file2 = LoadedFiles[2];
Thus releasing the reference to the object you had just created. This will cause needless garbage collection to occur. It's better to just do File file2 = LoadedFiles[2] if it makes a difference to you stylistically.
Best place to look into reference types vs value types is the C# Language specification http://www.microsoft.com/en-us/download/details.aspx?id=7029 on page 77.
Upvotes: 6
Reputation: 77334
Lets start with your code:
File file2 = new File();
file2 = LoadedFiles[2];
The first line is redundant. You allocate a new file, just to say "hey, look, forget about that one, take the one from the array." Well, don't create a new File then:
File file2 = LoadedFiles[2];
Now, it looks like you might have copied whatever is in LoadFiles[2]. But you did not. All you copied was a reference
to that object. There still is only one object and now two variables know of it.
For comparison: Your friend lives in 123 Oak Street. You have that address in your address book. That's a reference. You don't have his real house in there, just a reference to his house so you can find it. When you copy that reference by sending it to another friends phone book, the reference was copied. The original house is still just a single house. By copying the address, there wasn't a second house built.
Upvotes: 0
Reputation: 273494
In
File file2 = new File();
file2 = LoadedFiles[2];
the copying is not important. Only a reference is being copied. But a needless instance of File()
is created. Merge it:
File file2 = LoadedFiles[2];
Upvotes: 3
Reputation: 62276
This is not definitely bad practice. And you don't copy an object, you just keep a reference on it (pointer of 4 bytes).
This is often related to a coding style. I personally don't like the first way, as it makes reading complicated and often I need to check the return value of the function, so having
va file2 = LoadedFiles[2];
is just convinient choice for me.
Also there is no meaning of writing
File file2 = new File();
file2 = LoadedFiles[2];
just write:
var file2 = LoadedFiles[2];
Upvotes: 1