|
-
Feb 2nd, 2006, 07:43 PM
#1
Re: Is There A Deconstructor For A C# Class Like In Visual C++?
This is basically how garbage collection works in .NET. Whenever there are no references remaining to an object on the heap, that object is tagged as available for garbage collection. This means that the object sits around in memory until the garbage collector decides it needs the space, at which point the object's Finalize method is called and it is cleaned up and the resources it held are reclaimed. Now let's say that that object was a String. The only resources that that object held was the space it occupied in memory, so there is no need for it to have a Dispose method because there is nothing for it to do. If, however, that object was a Form then it would be holding other resources, like handles and the like. If that Form's Dispose method was not called before the last reference to it lost scope then those resources would be unavailable to the system until the garbage collector decided to clean it up. That's why some classes have Dispose methods and others don't. If your class holds any unmanaged resources itself then it really should implement a Dispose method to release these resources. The more usual situation is that your class would have variables that refer to other objects that have Dispose methods. In this case you should implement a Dispose method to call those Dispose methods. This will cause a cascade effect so that no matter how deep the unmanaged resources are, they will be released by the object that holds them because its Dispose method will have been called. If you don't, those resources will be tied up unnecessarily until the garbage collector decides it needs the memory occupied by your object.
In short, if your class has variables of a type that have a Dispose method, you should also implement a Dispose method to call them. When you have finished with your or any other object, if it has a Dispose method then call it. If the variable that refers to the object loses scope very soon then that's all you have to do. If the variable doesn't or might not lose scope for some time then set the varaible to null. That removes the reference to the object and thus it becomes eligible for garbage collection.
-
Feb 3rd, 2006, 01:37 AM
#2
Hyperactive Member
Re: Is There A Deconstructor For A C# Class Like In Visual C++?
This is out of my Visual C# 2005 Book from Microsoft:
Code:
class Tally
{
public Tally()
{
this.instanceCount++;
}
~Tally()
{
this.instanceCount--;
}
public static int InstanceCount()
{
return this.instanceCount;
}
private static int instanceCount = 0;
}
You can Not declare a destructor in a Struct.A struct is a value type that lives on the stack and not the heap, so garbage collection does not apply..
struct Tally
{
~Tally() {...} // compile - time error
}
You cannot declare an access modifier (such as public) for a destructor.This is because you never call a destructor yourself - the garbage collector does.
public ~Tally() {...}// compile - time error
You never declare a destructor with parameters, and it cannot take any parameters AGain, this is because you never call a destructor yourself..
~Tally(int parameter) {...}//compile - time error
The compiler automatically translates a destructor into an override of the Object.Finalize method.The compiler translates the following destructor:
class Tally
{
~Tally() {...}
}
Into this:
class Tally
{
protected override void Finalize()
{
try{...}
finally {base.Finalize();}
}
}
The compiler - generated Finalize method contains the destructor body inside a try block, followed by a finally block that calls the base class Finalize.This ensures that a destructor always calls its base class destructor.It's important to realize that only the compiler can make this translation.You can not override Finalize yourself and you cant call Finalize yourself..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|