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.
You don't need an explicit cast if you use a
using
block. (This is the preferred idiom, where possible, for dealing withIDisposable
objects.)using (HttpListener hl = /* ... */) { // ... }
It has a
Close
method which is pretty-much an alias forDispose
. (Not my favourite pattern, but the framework designers seem to like it!)HttpListener hl = /* ... */ try { // ... } finally { hl.Close(); }
精彩评论