开发者

System.Net.HttpListener only explicitly implements IDisposable

开发者 https://www.devze.com 2023-03-10 12:32 出处:网络
Why does HttpListener explicitly implement IDisposable.This means you have to cast to IDisposable before calling d开发者_如何学Pythonispose and in my opinion makes the fact you have to call dispose le

Why does HttpListener explicitly implement IDisposable. This means you have to cast to IDisposable before calling d开发者_如何学Pythonispose and in my opinion makes the fact you have to call dispose less obvious.


  1. You don't need an explicit cast if you use a using block. (This is the preferred idiom, where possible, for dealing with IDisposable objects.)

    using (HttpListener hl = /* ... */)
    {
        // ...
    }
    
  2. It has a Close method which is pretty-much an alias for Dispose. (Not my favourite pattern, but the framework designers seem to like it!)

    HttpListener hl = /* ... */
    try
    {
        // ...
    }
    finally
    {
        hl.Close();
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号