Hi All
What is the use of garbage control?, when we go for this? i need answer with small example, thanks in advance
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Joginder BangerRank #360 · 7K reputationPosted Nov 12, 2014, 11:37 PM EST
I assume you are familiar with c and C++ language. Suppose we are not assign any number
int i; By default value assign like '213245' In c language
int i; By default value assign 0. Not a garbage value.
In c language developer (programmer) worry about the memory because in c language have no garbage control.
In C# language no worry about the memory.
VulpesRank #55 · 29.2K reputationPosted Nov 13, 2014, 2:32 AM EST
The output is:
After MyMethod() ends, there are no longer any references to the MyClass object and so it is marked for garbage collection. However, as there is no pressure to allocate new objects, its destructor is not run and its memory reclaimed until after a key is pressed.
Normally, it doesn't matter much when the GC runs. However, in some cases, this indeterminate behavior can be a problem and in those cases you can trigger an immediate garbage collection by calling GC.Collect().
So, if you change the Main method to this:
the output now is :
showing that the object has now been de-allocated before the key is pressed.