Life Cycle of Object

来源:互联网 发布:策划 知乎 编辑:程序博客网 时间:2024/05/14 20:20

 

Garbage collection, managed heap.

 

Set object to null.

Application root.

Generation.

 

System.GC type

GC.Collect();

GC.WaitForPendingFinalizers();

 

 

class A

{

~A()

{

//clear unmanaged resources.

}

}

 

public interface IDisposable

{

void Dispose();

}

public class A:IDisposable

{

Private bool disposed=false;

public void Dispose()

{

//clear unmanaged resources.

}

private void CleanUp(bool disposing)

{

If(!this.disposed)

{

if(disposing)

{

//free managed resources.

}

}

disposed=true;

}

~A()

{

//clear unmanaged resources.

CleanUp(false);

}

 

}

 

Using(A a=new A())

{

//automatically call Dispose() when exit.

}

原创粉丝点击