开发者

System.Net.HttpListener only explicitly implements IDisposable

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜