-What is the purpose of having destructor
-It is said that if you do not explicitly create a destructor method for a class, C# automatically provides one. If C# will automatically provide one what is purpose of creating it.
Anyone knows please explain
Loading
Posted Dec 2, 2011, 4:28 PM
Sam HobbsPosted Dec 2, 2011, 4:06 PM
Posted Dec 2, 2011, 11:06 AM
VulpesPosted Dec 2, 2011, 10:23 AM
Incidentally, what you're seeing (or rather not seeing) at the moment is a good demonstration of object destruction being non-deterministic in .NET. You just can't be sure when the destructor will be called and, here the Friend class destructor is getting called after the call to Console.ReadKey. It didn't do that when I ran it from the command line but I'm not attaching a debugger which slows things down.
Posted Dec 2, 2011, 10:09 AM
VulpesPosted Dec 2, 2011, 9:34 AM
Posted Dec 2, 2011, 9:11 AM
VulpesPosted Dec 2, 2011, 5:14 AM
However, Sam's right that if you're using VS, then 'Start Without Debugging' (Ctrl-F5) will prevent the console from closing before you've read the output.
The addition of Console.ReadKey() doesn't help here in debug mode (i.e. the console is blank) because the CLR hasn't called any of the destructors when that line is executed. However, if you insert the highlighted line before Console.ReadKey(), then it will force an immediate garbage collection and you should then see the output:
static void Main()
{
Friend f = new Friend();
Child c = new Child(f);
c = null;
GC.Collect();
Console.ReadKey();
}
Sam HobbsPosted Dec 1, 2011, 10:37 PM
You can execute the program without debugging using Ctrl-F5 or by using "Debug | Start without debugging". That way you do not need to have the Console.ReadKey(). I doubt that Console.ReadKey(); is the problem, however. Also, instead of using Console.ReadKey(); I just put a breakpoint (F9) at the end of Main and then I have the opportunity to look at the output.
Something else you can do using the debugger is to single-step through the program. If you do not know how to use breakpoints, then find articles about the debugger that explains about breakpoints. "Single-stepping" means that you execute one statement of source code at a time so you can watch what is happening. In this case, however, the debugger might not help so much; so you need to put breakpoints in the lines where Vulpes has the calls to Console.WriteLine and that way you can verify that the code is executed.
The important thing is that you need to check at least one more time to ensure that you have accurately coped all of Vulpes's code.
Posted Dec 1, 2011, 5:16 PM
Sam HobbsPosted Dec 1, 2011, 5:09 PM
Posted Dec 1, 2011, 4:52 PM
Posted Dec 1, 2011, 4:37 PM
static void Main()
{
Friend f = new Friend();
Child c = new Child(f);
c = null;
Console.ReadKey();
}
Sam HobbsPosted Dec 1, 2011, 4:32 PM
An over-simplified explanation is that beginners learning C# don't need to know about finalizers or destructors; the garbage collection in .Net and C# will do everything for you automatically. Do not try to do it yourself until you have had the opportunity to learn about how garbage collection, destructors and a finalizers work in .Net and C#.
VulpesPosted Dec 1, 2011, 2:15 PM
The output should be:
The explanation is as follows:
1. When 'c' is set to null, it becomes eligible for garbage collection.
2. Just before the Child object is destroyed, the CLR calls its destructor.
3. The Child destructor calls its Friend object's Dispose method (first line in output) and then sets 'f' to null so the Friend object becomes eligible for garbage collection.
4. The Child destructor then prints to the console (2nd line in output) and implicitly calls its base class destructor which also prints to the console (3rd line in output).
5. Finally, the Friend object's destructor gets called and prints to the console (4th line in output). Technically, it then implicitly calls System.Object's destructor (as it has no other base class) but this does nothing and so nothing is printed for that.
Posted Dec 1, 2011, 1:51 PM
Posted Dec 1, 2011, 12:32 PM
VulpesPosted Dec 1, 2011, 11:56 AM
A better name for it would have been a finalizer because in fact the C# compiler translates it to an override of the virtual protected Finalize() method which all classes inherit from System.Object. It also inserts a call to the base class's Finalize() method if it has one.
C# doesn't in fact provide a destructor if you don't provide one yourself. Possibly, you are thinking of an instance constructor?
The destructor is called automatically by the runtime. It cannot be called directly from C#.