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

Automatic Destruction

why is the object dog automatically destroyed? Does this happen when the program has finished running? where is the command to destroy the object?

18th Apr 2017, 6:28 AM
MatthewSmith
MatthewSmith - avatar
2 Answers
+ 9
An object is destroyed once its scope has ended. Objects and variables only last for the code block they're inside (a method, if/for/while, class, etc.). Ex: If the dog object were declared inside a method, once that method ended, that dog object would no longer be usable and would be destroyed.
18th Apr 2017, 6:59 AM
Tamra
Tamra - avatar
+ 2
I believe this is something the C# language is known for - its automatic garbage disposal
18th Apr 2017, 7:27 AM
Souna
Souna - avatar