How can I print the number of instances after I deleted a constructor? Since by me it shows the same number before and after GC.
Console.WriteLine("Number of instances: {0}", Book.readNumber());
b2 = null;
GC.Collect();
Console.WriteLine("Number of instances after GC: {0}", Book.readNumber());
I have 4 instances coming in through readNumber, but it still outputs 4 instead of 3 after GC the b2.
The requested Book class:
class Book
{
public String ISBN;
public String Author;
public double Price;
private static int Quantity;
public static int readNumber() { return Quantity; }
public Book ()
{
this.ISBN = "no ISBN"; this.Author = "no Author"; this.Price = 0.0;
Quantity++;
}
~Book() { Quantity--; }//first attempt to reduce the instance counter by 1
}
With Quantity, I count the instances from the class and save it in count for use in main.