Can we destroy an instantiated object manually and call its destructor before we exit the scope which it was declared? (C#) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we destroy an instantiated object manually and call its destructor before we exit the scope which it was declared? (C#)

In the course they showed a piece of code like this: Dog d = new Dog(); and it would output this: "Animal created. Dog created. Dog destroyed. Animal destroyed." I tried doing this: d = null; GC.Collect(); Console.WriteLine("Dog"); But the Dog and Animal destructors are always called last when program ends, is there a way to call them manually? I hope I explained well.

15th Feb 2017, 9:58 PM
SIMOMEGA
SIMOMEGA - avatar
2 Answers
+ 1
Override the Finalize(). https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx
16th Feb 2017, 4:55 AM
K.C. Leung
K.C. Leung - avatar
0
Thx!
16th Feb 2017, 9:29 AM
SIMOMEGA
SIMOMEGA - avatar