Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is there no Dispose method on HttpWebResponse

HttpWebReponse implements IDisposable interface, but why is there no Dispose method. It only contains Close method. Will be using pattern still available for this class?

like image 323
user705414 Avatar asked Nov 09 '11 10:11

user705414


People also ask

How do you use the dispose method?

The Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.

What happens if you dont dispose IDisposable?

IDisposable is usually used when a class has some expensive or unmanaged resources allocated which need to be released after their usage. Not disposing an object can lead to memory leaks.

What is a Dispose method?

Dispose improves performance and optimizes memory by releasing unmanageable objects and scarce resources, like Graphics Device Interface (GDI) handles used in applications with restricted Windows space. The Dispose method, provided by the IDisposable interface, implements Dispose calls.

Can we call Dispose method in C#?

A developer can call Dispose on an instance of this class to release the memory resource held by the database connection object. After it is freed, the Finalize method can be called when the class instance needs to be released from the memory.


1 Answers

HttpWebResponse implements IDisposable interface explicitly. So you can call Dispose only when you cast HttpWebResponse to IDisposable. The Close method of HttpWebResponse calls Dispose internally.

HttpWebResponse response = // assigned from somewhere
IDisposable disposableResponse = response as IDisposable;

disposableResponse.Dispose();

Since HttpWebResponse implements IDisposable you can use it with an using-statement.

HttpWebResponse response = // assigned from somewhere

using(response) {
  // do your work;
}
like image 97
Jehof Avatar answered Oct 25 '22 05:10

Jehof