TcpClient メンバーを持つクラスがあります。例えば:
class CustomNetClient
{
TcpClient tcp;
NetworkStream ns;
//...
}
きちんと閉まっていることを確認したい。そのように実装されましたIDisposable
:
class CustomNetClient
{
TcpClient tcp;
NetworkStream ns;
public CustomNetClient()
{
tcp = new TcpClient("1.1.1.1",80);
ns = tcp.GetNetworkStream();
}
public void Dispose()
{
tcp.Close();
ns.Close();
}
//...
}
アプリケーションではCustomNetClient
withを呼び出しますusing
。
//...
using(CustomNetClient nc=new CustomNetClient)
{
// This will be a long long process, connection will stay open
}
これは適切で十分な方法ですか、それとも提案や懸念事項はありますか?