Monday, 7 January 2013

To count total number of objects is created for any Class in c# or Asp.net

public class Class1
    {
        static int Count = 0;

        public Class1()
        {
            Count++;
        }

        ~Class1()
        {
            Count--;
        }

        public static int getCount()
        {
            return Count;
        }

    }

class Program
    {
        static void Main(string[] args)
        {

            Class1 obj;

            for (int i = 0; i < 10; i++)
            {
                obj = new Class1();
                Console.WriteLine("Current Count" + Class1.getCount());
            }

            Console.ReadLine();
        }
    }

No comments:

Post a Comment