C# 需要在使用TcpClient的类上实现终结器吗?
我有一个类(比如C# 需要在使用TcpClient的类上实现终结器吗?,c#,idisposable,finalizer,unmanagedresources,C#,Idisposable,Finalizer,Unmanagedresources,我有一个类(比如MyClass),它使用TcpClient对象(has作为私有字段)MyClass实现IDisposable调用TcpClient。在Dispose方法中关闭 我的问题是,MyClass是否也应该实现一个终结器来调用Dispose(bool Disposing)以释放TcpClient的非托管资源,以防调用代码调用MyClass.Dispose 谢谢是的,你应该- 请记住,安全带和吊带密码永远不会让你在凌晨2点被叫进办公室:)不,你不应该这样做。 因为您不应该在终结器中对其他对象
MyClass
),它使用TcpClient
对象(has作为私有字段)MyClass
实现IDisposable
调用TcpClient。在Dispose
方法中关闭
我的问题是,MyClass
是否也应该实现一个终结器来调用Dispose(bool Disposing)
以释放TcpClient的
非托管资源,以防调用代码调用MyClass.Dispose
谢谢是的,你应该-
请记住,安全带和吊带密码永远不会让你在凌晨2点被叫进办公室:)不,你不应该这样做。
因为您不应该在终结器中对其他对象调用方法,所以它可能在您的对象之前被终结
垃圾收集器将调用TcpClient的终结器,所以让他来做吧
Dispose中的模式是:
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources (here your TcpClient)
}
// dispose your unmanaged resources
// handles etc using static interop methods.
}
不,你不必。TcpClient是围绕非托管套接字的包装器类,因此,它以应该被处置的方式进行管理。你所做的已经足够了。不,你不应该
从优秀职位:
最后定稿从根本上说是必要的
不同于结束对象的
一生。从正确的角度
视图之间没有排序
终结器(特殊情况外)
对于关键终结器),如果
有两个GC认为的对象
你不能同时死去
预测将完成的终结器
第一。这意味着你不能有一个
与任何
实例中存储的可终结对象
变量
这是我对一次性/最终确定模式的参考实现,并附有说明何时使用的注释:
/// <summary>
/// Example of how to implement the dispose pattern.
/// </summary>
public class PerfectDisposableClass : IDisposable
{
/// <summary>
/// Type constructor.
/// </summary>
public PerfectDisposableClass()
{
Console.WriteLine( "Constructing" );
}
/// <summary>
/// Dispose method, disposes resources and suppresses finalization.
/// </summary>
public void Dispose()
{
Dispose( true );
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes resources used by class.
/// </summary>
/// <param name="disposing">
/// True if called from user code, false if called from finalizer.
/// When true will also call dispose for any managed objects.
/// </param>
protected virtual void Dispose(bool disposing)
{
Console.WriteLine( "Dispose(bool disposing) called, disposing = {0}", disposing );
if (disposing)
{
// Call dispose here for any managed objects (use lock if thread safety required), e.g.
//
// if( myManagedObject != null )
// {
// myManagedObject.Dispose();
// myManagedObject = null;
// }
}
}
/// <summary>
/// Called by the finalizer. Note that if <see cref="Dispose()"/> has been called then finalization will
/// have been suspended and therefore never called.
/// </summary>
/// <remarks>
/// This is a safety net to ensure that our resources (managed and unmanaged) are cleaned up after usage as
/// we can guarantee that the finalizer will be called at some point providing <see cref="Dispose()"/> is
/// not called.
/// Adding a finalizer, however, IS EXPENSIVE. So only add if using unmanaged resources (and even then try
/// and avoid a finalizer by using <see cref="SafeHandle"/>).
/// </remarks>
~PerfectDisposableClass()
{
Dispose(false);
}
}
//
///如何实现dispose模式的示例。
///
公共类PerfectDisposableClass:IDisposable
{
///
///类型构造函数。
///
公共PerfectDisposableClass()
{
Console.WriteLine(“构造”);
}
///
///Dispose方法,处置资源并抑制终结。
///
公共空间处置()
{
处置(真实);
总干事(本);
}
///
///处理类使用的资源。
///
///
///如果从用户代码调用,则为True;如果从终结器调用,则为false。
///如果为true,则还将为任何托管对象调用dispose。
///
受保护的虚拟void Dispose(bool disposing)
{
WriteLine(“Dispose(bool disposing)调用,disposing={0}”,disposing);
如果(处置)
{
//对于任何托管对象,请在此处调用dispose(如果需要线程安全,请使用锁),例如。
//
//如果(myManagedObject!=null)
// {
//myManagedObject.Dispose();
//myManagedObject=null;
// }
}
}
///
///由终结器调用。请注意,如果已调用,则终结将
///已被暂停,因此从未致电。
///
///
///这是一个安全网,以确保我们的资源(托管和非托管)在使用后得到清理
///我们可以保证在某个时刻调用终结器,前提是
///没有打电话。
///但是,添加终结器的成本很高。因此,只有在使用非托管资源时才添加终结器(甚至可以尝试)
///并避免使用终结器)。
///
~PerfectDisposableClass()
{
处置(虚假);
}
}
欢迎对我的Perfect(我一点也不相信,但这是我迄今为止最好的一次)课程发表任何评论。