Csharp.Net output type question

Q.  Trace the output
class Myclass
{
   public  int count = 0;
   public  Myclass()
    {
        count++;
    }
}


    class Program
    {
        static void Main(string[] args)
        {
            Myclass obj1 = new Myclass();
            Console.WriteLine(obj1.count);

            Myclass obj2 = new Myclass();
            Console.WriteLine(obj2.count);

            Myclass obj3 = new Myclass();
            Console.WriteLine(obj3.count);
        }

    }

- Published on 31 Aug 15

a. 1, 1, 1
b. 1, 2, 3
c. 0, 1, 2
d. All of the above

ANSWER: 1, 1, 1
 
In the above example three different objects are created of class Myclass. So every time count is reinitialized with zero and constructor will be called. Here count variable is non - static therefore separate copy of count will be created for each object. So the answer will be 1 1 1.

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)