constructor and destructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

constructor and destructor

the foll code in the course dhows o/p as "constructor" & "destructor"...but actually when i run the code it shows only constructor. namespace SoloLearn { class Program { class Dog { public Dog() { Console.WriteLine("Constructor"); } ~Dog() { Console.WriteLine("Destructor"); } } static void Main(string[] args) { Dog d = new Dog(); } } } what is the problem

5th Nov 2016, 4:10 AM
Vinay Diddi
Vinay Diddi - avatar
1 Answer
+ 1
The destructor is probably called when the program ends, so it is not printed out. This is because of the scope : you object exists in the main, but is only destroyed after the main is left. If you create a loop in which you call the object, you should be able to see the destructor.
5th Nov 2016, 11:52 AM
Pierre Varlez
Pierre Varlez - avatar