using System;class Test
{
Test()
{
Console.WriteLine("Test的构造数");
}
~Test()
{
Console.WriteLine("Test的析构函数");
} void PrintAMessage(string msg)
{
Console.WriteLine("PrintAMessage:{0}", msg);
}
void Dispose()
{
Finalize();
GC.SuppressFinalize(this);
} static void Main()
{ }
}为什么在调用Finalize();方法时,会出错,
Finalize();方法不是在System名字空间下的Object类中嘛!
为什么不可以直接调用,还需要using别的名字空间吗?
{
Test()
{
Console.WriteLine("Test的构造数");
}
~Test()
{
Console.WriteLine("Test的析构函数");
} void PrintAMessage(string msg)
{
Console.WriteLine("PrintAMessage:{0}", msg);
}
void Dispose()
{
Finalize();
GC.SuppressFinalize(this);
} static void Main()
{ }
}为什么在调用Finalize();方法时,会出错,
Finalize();方法不是在System名字空间下的Object类中嘛!
为什么不可以直接调用,还需要using别的名字空间吗?
解决方案 »
- 请问这个API怎么调用啊
- 有关pictureBox的简单问题
- OracleDataReader reader中read[]数据的问题
- timer怎么不好使啊
- 使用控件,dataGridView,如何使整个表格填满控件?求助
- 使用WindowsService服务启用进程的问题
- wpf中如何更改listview某一项的文字颜色?
- 夜深了,我还在.这里为大家提供一个不错的ftp,不少实用的.NET学习资料
- 关于datagrid里的button
- (请教高手)动态生成的CheckBox的取值问题!
- 请问下,如何在程序中用递归方法实现这个问题?
- .net 2005下的TIMER控件为什么没有autoreset属性啊?
您无法在 C# 或 C++ 编程语言中重写 Finalize 方法。在 C# 中可使用析构函数语法实现 Finalize 方法。在 .NET Framework 2.0 版中,C++ 为实现 Finalize 方法提供了自己的语法,如 Destructors and Finalizers in Visual C++ 中所述。在早期版本中,C++ 与 C# 一样也使用析构函数语法来实现 Finalize 方法。
如果需要实现dispose 应该如下 首先继承IDispose接口
// Design pattern for a base class.
public class Base: IDisposable
{
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
} // Use C# destructor syntax for finalization code.
~Base()
{
// Simply call Dispose(false).
Dispose (false);
}
}
// Design pattern for a derived class.
public class Derived: Base
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Release managed resources.
}
// Release unmanaged resources.
// Set large fields to null.
// Call Dispose on your base class.
base.Dispose(disposing);
}
// The derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits
// them from the base class.
}