C# vs C++/CLI dispose

zbzpo2002 2010-07-03 05:37:53
//C#
using System;

public class TestClass1:IDisposable
{
public TestClass2 t2_;
public TestClass3 t3_;
public TestClass1()
{
t2_ = new TestClass2();
t3_ = new TestClass3();

}


public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass1");
}

}


public class TestClass2:IDisposable
{
public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass2");
}

}

public class TestClass3:IDisposable
{
public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass3");
}

}




public class Program
{
public static void Main()
{
using ( TestClass1 ww = new TestClass1())
{

}
}
}

输出:
I'm the dispose in TestClass1

...全文
150 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
不一样的 C++遵循的是RAII的原则
其实就是获取资源 即 初始化,出了生命周期自动调用析构,所以是确定性析构
GC需要额外的系统线程,而smart_pointer 不过是根据以上C++原则设计好的C++的class而已,所以没有额外的线程的。

有些重要的资源是必须立即回收的,我猜所以.NET才会有Dispose吧,因为GC的调用是不确定的。
俺也是刚刚是学C#,不过C#还是很漂亮的,就是非托管资源的管理,我还不是太习惯.
C++强调把资源放进一个Class内管理,在出了生命周期的时候自动就调用析构:
所以我通常会写这样的程序


int main()
{
{
Unmanagered resource("!!!!!"); //假设这里获得了资源,在Stack上
} // 这里自动调用析构,就释放掉了

}





兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
恩。这并不是c#麻烦。其实我不太了解,c++的智能指针是不是和.net的垃圾回收差不多?
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
我2楼的时候就已经意识到了 嘿嘿
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
那你其实可以参考8楼的代码。
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
俺就是不想麻烦GC,到时候再把它 SuppressFinalize 起来。
似乎C++/CLI的实现更漂亮些
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
谢了!
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
I'm the dispose in TestClass1
I'm the TestClass3 Finalizer!
I'm the TestClass2 Finalizer!
I'm the TestClass1 Finalizer!

Class内部的member的Dispose不会被自动调用的,似乎还是要手动调用。
俺一直写C++程序,只是觉得有点不习惯
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
不用看结果。
没太大意义。看看垃圾回收的原理介绍吧。看看有IDispose和没有的区别。析构函数的执行顺序。
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
using System;

public class TestClass1:IDisposable
{
public TestClass2 t2_;
public TestClass3 t3_;
public TestClass1()
{
t2_ = new TestClass2();
t3_ = new TestClass3();

}


public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass1");
}

~TestClass1()
{
Console.WriteLine("I'm the TestClass1 Finalizer!");
}

}


public class TestClass2:IDisposable
{
public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass2");
}

~TestClass2()
{
Console.WriteLine("I'm the TestClass2 Finalizer!");
}

}

public class TestClass3:IDisposable
{
public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass3");
}

~TestClass3()
{
Console.WriteLine("I'm the TestClass3 Finalizer!");
}

}

public class Program
{
public static void Main()
{
using ( TestClass1 ww = new TestClass1())
{

}
Console.ReadLine();
}
}


看看这程序的执行结果
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
希望能在Dispose的时候就都执行,你就需要扩展你的Dispose方法,依次的释放成员变量。否则就只能等到垃圾回收执行到析构函数的时候,使得成员变量的对象无效,调用成员变量的Dispose。

public class TestClass1 : IDisposable
{
public TestClass2 t2_;
public TestClass3 t3_;
public TestClass1()
{
t2_ = new TestClass2();
t3_ = new TestClass3();
}

public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass1");
t2_.Dispose();
t3_.Dispose();
}
}
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
public class TestClass1 : IDisposable
{
public TestClass2 t2_;
public TestClass3 t3_;
public TestClass1()
{
t2_ = new TestClass2();
t3_ = new TestClass3();
}

public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass1");
}
}


你的Dispose方法中没有依次的调用t2,t3的Dispose,建议你看看垃圾回收的原则。
第一次是调用Dispose,此时就调用Dispose方法输出一行,但其余的2个对象,要在TestClass1的析构时候才会执行Dispose
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
哈哈 感谢楼上。
只是那不是析构,而是终结器。
C++/CLI 中的析构会自动调用Dispose方法,而C#中的析构实际上是终结器Finalizer。
俺在想提高程序性能的角度看的话,是不是在Dispose中处理掉就不劳烦GC了。
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
另外。你1楼贴的和你最初的代码不同。你如果按我写的这样。c#中也用析构函数就可以了。
你只是用的vc的编译器,写的还是托管代码。和你最初的没区别。最初的也写析构函数才能对比测试。
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复

using System;

public class TestClass1
{
public TestClass2 t2_;
public TestClass3 t3_;
public TestClass1()
{
t2_ = new TestClass2();
t3_ = new TestClass3();
}

~TestClass1()
{
Console.WriteLine("I'm the dispose in TestClass1");
}

}

public class TestClass2
{
~TestClass2()
{
Console.WriteLine("I'm the dispose in TestClass2");
}

}

public class TestClass3
{
~TestClass3()
{
Console.WriteLine("I'm the dispose in TestClass3");
}

}

public class Program
{
public static void Main()
{
TestClass1 ww = new TestClass1();
}
}


析构函数
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
我还要手动调用两个成员的Dispoe啊,C#麻烦啊。
For class TestClass1

public void Dispose()
{
Console.WriteLine("I'm the dispose in TestClass1");
t2_.Dispose();
t3_.Dispose();
}
zbzpo2002 2010-07-03
  • 打赏
  • 举报
回复
using namespace System;


public ref class TestClass2
{
public:
~TestClass2()
{
Console::WriteLine("I'm the dispose in TestClass2");
}

};

public ref class TestClass3
{
public:
~TestClass3()
{
Console::WriteLine("I'm the dispose in TestClass3");
}

};

public ref class TestClass1
{
public:

TestClass2 t2_;
TestClass3 t3_;

TestClass1()
{
}

~TestClass1()
{
Console::WriteLine("I'm the dispose in TestClass1");
}

};


int main()
{
{
TestClass1 t1;
}
}


输出:
I'm the dispose in TestClass1
I'm the dispose in TestClass3
I'm the dispose in TestClass2

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧