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.
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.
Dispose is to release all external resources, string and int are simple data types and have no external resources.
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.
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.
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.