Monday, May 24, 2010

I have this program in c# for constructors.it gives the output as 12 &13.can any1 tell me how its possible?

using System;


namespace CalculateNumber


{


class Calculate


{


static int number1;





public void Display(int number)


{


Console.WriteLine(number);


}


Calculate()


{


number1++;


Display(number1);


}


static Calculate()


{


number1 = 10;


number1++;


}





static void Main()


{


Calculate cal1 = new Calculate();


Calculate cal2 = new Calculate();


Console.ReadLine();


}


}


}

I have this program in c# for constructors.it gives the output as 12 %26amp;13.can any1 tell me how its possible?
static Calculate()


{


number1 = 10;


number1++;


}


here you have initialize the number1=10


then "number1++" will make it 11,





Calculate()


{


number1++;


Display(number1);


}


here you made it to number1++ so it will become 11+1=12;


and called Display Method,So it will display 12


Again you are calling the same function by instantiating it (cal2) so it will became12+1=13 and Display.....





Ans.Static Constructors will be executed only once as opposed to other constructors which are executed whenever an object of the class is created......


I hope i have clear your doubt............ss.pandey@tcs.com


No comments:

Post a Comment