tags:

views:

65

answers:

4

why dispose method is not for string object in c#? As we know Dispose() is the method for dispose the object. But why it is not allows for string object or integer object?

Edited: What does the mean of managed object ?. Please guide me.

+2  A: 

Dispose is to release all external resources, string and int are simple data types and have no external resources.

Sruly
+7  A: 

No, Dispose is the method to dispose resources not managed by the GC. String is just a regular, managed object and will thus be automatically reclaimed by garbage collection.

Brian Rasmussen
What does the mean of managed object ?. Please guide me.
Lalit
@Latit: A managed object is an instance of a reference type. It is allocated by instantiating the type, and the associated memory is automatically reclaimed during garbage collection at some point. As long as the object doesn't use any unmanaged resources (such as OS handles) there's no reason to even implement the IDisposable interface.
Brian Rasmussen
A: 

Disposing an object is not something you want to do, it is something you have to do. Memory is usually managed by the garbage collector, which frees unused memory. Unused means that you don't have any references in your application pointing to that object.

There are resources which can't be (or should be) managed by the garbage collector. They are called "unmanaged resources". For instance: a file on the disk or a connection to the database. They are opened and closed explicitly.

Dispose is a common way to release this resources. The using keyword is very helpful for this.

If you had to dispose strings and integers, your code would become very, very complicated.

Stefan Steinegger
A: 

Dispose is also necessary for objects that put 'hooks' into other objects (e.g. event handlers). If, for example, an object subscribes to change notifications for another object, it needs to let that other object know when it should no longer send such notifications.

supercat