3 c# clr dispose idisposable visual-studio
我有两个班,说阶级MyFirstClass和MyAnotherClass,MyAnotherClass正在实施IDiposable接口.
public class MyFirstClass
{
public string xyz{get;set;} ..... and so on
}
public class MyAnotherClass : IDisposable
{
private readonly MyFirstClass objFc = new MyFirstClass();
public static void MyStaticMethod()
{
var objOfFirstClass = new MyFirstClass();
// something with above object
}
public void MyNonStaticMethod()
{
// do something with objFc
}
#region Implementation of IDisposable
.... my implementations
#endregion
}
Run Code Online (Sandbox Code Playgroud)
现在我还有一个我打电话的课MyAnotherClass,就像这样
using(var anotherObj = new MyAnotherClass())
{
// call both static and non static methods here, just for sake of example.
// some pretty cool stuffs goes here... :)
}
Run Code Online (Sandbox Code Playgroud)
所以我想知道,我应该担心我的对象的清理方案吗?此外,我的ObjFC(非静态内部)和objOfFirstClass(内部静态)会发生什么.
AFAIK,使用将照顾一切...但我需要知道更多...
objOfFirstClass是方法中的局部变量.退出方法后,它将有资格进行垃圾回收.它不会被处理,因为它没有实现IDisposable.
objFc当其父对象超出范围时,将有资格进行垃圾收集.同样,这与处理它无关.
DisposeIDisposable当除了简单的内存管理之外有清理时使用/ .CLR使用垃圾收集处理为您清理内存.using确保实现的对象在完成后调用IDisposable其Dispose方法是一种很好的方法- 但如果你所有的都是内存管理,则不需要使用它.
| 归档时间: |
|
| 查看次数: |
1330 次 |
| 最近记录: |