I hope these code samples are just incomplete, but because of that the question makes no sense.
In first example, the value of
objb
becomes
unreachable immediately, and hence the instance becomes a subject of Garbage Collection. It is ineffective only in one sense: you make redundant operation.
The second example makes no sense, because on second and next calls you simply replace the instance of an object with another instance, and it makes the previous instance unreachable and then — everything goes like in the previous case.
If you write the code which makes sense, you will eliminate the difference between the cases. You would have a collection of
B
instances and add one more instance each time you call
Add
. In this case, you cannot offer two different implementations, so your question disappears. This way, your question lacks subject.
But your idea to analyze performance in different ways of implementation is good. My advice is this: in each case: 1) disassemble your code using ILDASM.EXE, 2) time execution using
System.Diagnostics.Stopwatch
. Please see:
http://msdn.microsoft.com/en-us/library/f7dy01k1%28v=vs.100%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[
^].
—SA