V K

V K

  • NA
  • 1
  • 1.5k

destructor

Apr 24 2013 10:58 AM
1)  Create a Employee Class with members say fullaname, EmpID , CurrPAy with Appropriate Constructors and Destructors .Write a driver program to create 4 employees and at each time of the creation of a new employee display the count of objects created with the help of constructor .Also destroy the objects with the help of destructor and Garbage Collector. At each time of the destruction display the number of objects destroyed and number of objects remaining.

i don't know why my objects are not getting destroyed.

My solution:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
  class Employee : IDisposable
  {
  public string fullname;
  public string empid;
  public float currpay;
  public static int count=0;


  public Employee(string fname, string id, float pay)
  {
  count = count + 1;
  fullname = fname;
  empid = id;
  currpay = pay;
  }
 
  ~Employee()
  {
  count = count - 1;
  }
 
  public void Dispose()
  {
  System.GC.SuppressFinalize(this);
  }
  } 
  public class MainClass
  {
  public static void Main()
  {
  Employee e1 = new Employee("A","1",5000);
  Employee e2 = new Employee("B","2",6000);
  Employee e3 = new Employee("C","3",7000);
  Employee e4 = new Employee("D","4",2000);
  Console.WriteLine("Constructor called {0} times ",Employee.count);
  e1 = null;
  e1.Dispose();
  e3.Dispose();
  GC.Collect();
  Console.WriteLine("Constructor called {0} times ", Employee.count);
  }
  }
 
 

Answers (1)