Reputation: 53
Last week, i made a code interview.
My interviewer said: "What do you prefer between
var byteArray = myFunction();
and
byte[] Array = myFunction();" ?
I answered, once compiled, theses lines give the same result.
And my interviewer said: "Yes, but they are not garbaged the same way: var byteArray is better because GC garbages the variable quickly."
I'm very astonished that two lines of code giving the same result once compiled are not garbaged the same way, but maybe i'm wrong.
So: Are they garbaged the same way or not ? If not, can you explain me why ?
Upvotes: 0
Views: 177
Reputation: 234795
Your interviewer was incorrect.
In your particular case, var
stands in for byte[]
. The code is equivalent.
Perhaps your interviewer was thinking, in error, that the first case in a sense creates one object, but the second case creates several. Hogwash, of course.
Upvotes: 4
Reputation: 63772
They are exactly the same, assuming there's no implicit conversion to byte[]
involved. There's no way for them to be different, since var
is just syntactic sugar that gets replaced with the proper type.
Upvotes: 4
Reputation: 157058
Yes, they are garbage collected the same way. Same types, same behavior. This is a compiler feature: once compiled, there is nothing what makes the one code different than the other.
Upvotes: 6