why is there no Dispose method on HttpWebResponse

前端 未结 1 1477
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 09:13

HttpWebReponse implements IDisposable interface, but why is there no Dispose method. It only contains Close method. Will

1条回答
  •  执笔经年
    2021-01-11 09:45

    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;
    }
    

    0 讨论(0)
提交回复
热议问题