Why the output is: "Constructor Constructor Destructor Destructor"??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the output is: "Constructor Constructor Destructor Destructor"???

namespace ConsoleApplication7 { class Program { static void Main(string[] args) { Test one = new Test(); Test two = new Test(); } } } class Test { public Test() { Console.WriteLine("Constructor"); } ~Test() { Console.WriteLine("Destructor"); } }

6th Aug 2017, 11:04 AM
Raffi Ohannesian
Raffi Ohannesian - avatar
2 Answers
+ 3
one is created. Constructor is called, and "Constructor" is outputted. two us created, and the same thing happens. Then, the destructor is called for both at the end of the program, and "Destructor" is outputted twice. Hope this helps.
6th Aug 2017, 11:34 AM
J.G.
J.G. - avatar
+ 1
thanks
6th Aug 2017, 2:04 PM
Raffi Ohannesian
Raffi Ohannesian - avatar