Dispsose() or Close()
-
hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS
TJS4u wrote:
this.
Well it depends what Type "this" is. In some cases Streams/IO and such, they are fairly comparable. Dispose will normally call close. If an object is disposable you should always call dispose when you are finished with it to ensure the finaliser is suppressed. Normally it's ok to just call dispose, but there is no harm in closing explicitly first just to be safe. For some types of objects, like Forms, close has a specific meaning. Close() will close the form. Dispose will probably also call close, but in many cases you may want to close the form, but still keep the object to do some work with before you are ready to dispose of it.
Simon
-
hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS
Close() might eventually call Dispose(), actually depends on the class implementation, whereas Dispose must be defined if IDisposable is implemented for resource cleanup Use using clause; using(resource) { } This will automatically dispose your resource (say a stream), if it implements IDisposable interface
Regards, Lev
-
hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS
Close and Dispose for most classes seem to be very close. And in many cases Dispose will call Close. But, the fundamental difference is that Close has the class Dispose alot of its objects, while Dispose actually Disposes the object you are calling it on.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
-
hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS
As it was already said it depends on actual type. For example with sqlconnection object close() and dispose() do the same thing. For other types you can use Reflector to see what dispose() and close() do and compare them.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS
To find the difference between the two in a specific instance, .NET Reflector on the type you'd like to examine. You can download it for free from redgate.com. Scott P
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin