Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System; 
class MyClass { 
  int a, b; // private 
  // Create a class factory for MyClass. 
  public MyClass Factory(int i, int j) { 
    MyClass t = new MyClass(); 
    t.a = i; 
    t.b = j; 
    return t; // return an object 

public void Show() { 
    Console.WriteLine("a and b: " + a + " " + b); 
class MakeObjects { 
  static void Main() {   
    MyClass ob = new MyClass(); 
    int i, j; 
    // Generate objects using the factory. 
    for(i=0, j=10; i < 10; i++, j--) { 
      MyClass anotherOb = ob.Factory(i, j); // make an object 

There are 2 questions but inter-linked:

1.when an object is returned, like when t is returned in the above code, it should be by reference. Hence, will I be right to say that at that instance of the for loop, anotherOb and t are both reference to a new object?

2. If so, when the anotherOb goes out of scope for every for loop iteration, there will not be any garbage collection since t is still a reference to that object.Is it true since garbage collection takes place when there is no reference to the object?

1 solution

1) From the code of Factory we can see that a new object is created and assigned to t. This reference is returned and goes out of scope, but as it is assigned to anotherObj, anotherObj references the same instance. On each loop iteration, this variable goes out of scope (as it is declared insider loop); the referenced instance looses all references and is scheduled for removal. In each iteration of the loop a brand new instance is created, is shown and is scheduled for removal.

2) More exactly, GC performs destruction of object and reclaiming the memory according to its own strategy. You cannot assume any particular moment of type when it happens (and any code based on such assumption is incorrect). This behavior can be observed by creation a destructor and logging its calls. Also, this behavior can be modified by using System.GC.Collect, see[^]. It is not recommended to do so unless you have a really Cunning Plan :-).


Due to this GC behavior, the activity of writing of destructors makes little sense. With .NET, they are used pretty rarely.

Loosing all references to the object is not a trivial condition. Consider object A holds a reference to object B, object B holds a reference to object C, and object C holds a reference to object A. Consider you loose all other references to any of these objects except those circular references. Will these three object destroyed? Don't worry, the will, despite the fact they seemingly protect each other from destruction. They will be destroyed because there is no a way to access them from any actual code which could possibly be run later. So, GC mechanism is not trivial at all.

Share this answer
Wonde Tadesse 3-Aug-11 23:19pm    
Well explained. 5+
Sergey Alexandrovich Kryukov 3-Aug-11 23:36pm    
Thank you, Wonde.
Asish Limbu 3-Aug-11 23:58pm    
Nice Explanation.+5.
Sergey Alexandrovich Kryukov 4-Aug-11 0:18am    
Thank you, Asish.
RaviRanjanKr 4-Aug-11 8:47am    
Nice Answer, My 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900