Say, I have my own C# class defined as such:
public class MyClass
{
public MyClass()
{
//Do the work
}
~MyClass()
{
//Destructor
}
}
And then I create an instance of my class from an ASP.NET project, as such:
if(true)
{
MyClass c = new MyClass();
//Do some work with 'c'
//Shouldn't destructor for 'c' be called here?
}
//Continue on
I'd expect the destructor to be called at the end of the if
scope but it never is called. What am I missing?
When a destructor is called? Explanation: The destructor is called just before the object go out of scope or just before its life ends. This is done to ensure that all the resources reserved for the object are used and at last, are made free for others. 3.
yes, when you delete something, the destructor is called.
There is no such thing called 'constructors' and 'destructors' in C programming language or in structured languages, although there is no boundaries on defining such functions which act like them. You need to make functions which act like the constructors and destructors and then call them manually.
Destructors in C++ are members functions in a class that delete an object. They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc.
The equivalent to a C++ destructor is IDisposable
and the Dispose()
method, often used in a using
block.
See http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
What you are calling a destructor is better known as a Finalizer
.
Here's how you would use IDisposable. Note that Dispose()
is not automatically called; the best you can do is to use using
which will cause Dispose()
to be called, even if there is an exception within the using
block before it reaches the end.
public class MyClass: IDisposable
{
public MyClass()
{
//Do the work
}
public void Dispose()
{
// Clean stuff up.
}
}
Then you could use it like this:
using (MyClass c = new MyClass())
{
// Do some work with 'C'
// Even if there is an exception, c.Dispose() will be called before
// the 'using' block is exited.
}
You can call .Dispose()
explicitly yourself if you need to. The only point of using
is to automate calling .Dispose()
when execution leaves the using
block for any reason.
See here for more info: http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.110%29.aspx
Basically, the using
block above is equivalent to:
MyClass c = new MyClass();
try
{
// Do some work with 'C'
}
finally
{
if (c != null)
((IDisposable)c).Dispose();
}
There is no way you can control a timing or make a guess on when actually destructor of the object will be called. It's all up to the Garbage Collector
.
The only think you can be sure, in this case, that at the moment you leave that scope the object c
becomes unreachable (I assume there are no global references to that instance inside the scope), so the instance c
is intended to be identified and removed by Garbage Collector
when time comes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With