[英]Dispose instance
Class1:第一类:
public class FunctionBlocks
{
List<Hashtable> _htLogicalNodeList;
public FunctionBlocks()
{
_htLogicalNodeList = new List<Hashtable>();
FunctionBlock fb = new FunctionBlock();
fb.AddDODASignalList(new Hashtable);
_htLogicalNodeList.Add(fb.LogicalNodeHash);
fb = null;
}
}
Class2:类2:
public class FunctionBlock
{
Hashtable _htLogicalNode;
public FunctionBlock()
{
_htLogicalNode = new Hashtable();
}
public Hashtable LogicalNodeHash
{
get{return _htLogicalNode;}
set{_htLogicalNode = value;}
}
public void AddDODASignalList(Hashtable doDASignal)
{
_htLogicalNode.Add(doDASignal);
}
}
In this example I wan't to dispose "_htLogicalNode".在这个例子中,我不想处理“_htLogicalNode”。 "fb" object I have make it as null,Eventhough "FunctionBlocks" instance have "_htLogicalNode" references.
“fb” object 我已将其设为 null,尽管“FunctionBlocks”实例具有“_htLogicalNode”引用。 How I can dispose "_htLogicalNode" instance.
我如何处理“_htLogicalNode”实例。
Override dispose method and there you can make it null覆盖 dispose 方法,您可以将其设置为 null
What do you mean by "dispose"? “处置”是什么意思? You could have
FunctionBlock
implement IDisposable
in which case you could use the following:您可以让
FunctionBlock
实现IDisposable
在这种情况下,您可以使用以下内容:
using (FunctionBlock fb = new FunctionBlock())
{
fb.AddDODASignalList(new Hashtable);
_htLogicalNodeList.Add(fb.LogicalNodeHash);
}
However I can't see anything in FunctionBlock
that needs disposing, and so doing this would be pointless - the IDisposable
interface / pattern is essentially just a fancy / robust way of calling a method when you are done with an object.但是,我在
FunctionBlock
中看不到任何需要处理的内容,因此这样做毫无意义 - IDisposable
接口/模式本质上只是在完成 object 后调用方法的一种奇特/稳健的方式。 Unless you do something in the implemented Dispose
method then this does nothing.除非您在已实现的
Dispose
方法中执行某些操作,否则这不会执行任何操作。
If by "dispose" you mean free up the memory then the answer is that you don't need to do anything (you don't even need to set fb
to null).如果通过“处置”你的意思是释放 memory 那么答案是你不需要做任何事情(你甚至不需要将
fb
设置为 null)。 Simply let fb
go out of scope and the garbage collector will collect it and free up the used memory in its own time.只需让
fb
go 离开 scope,垃圾收集器就会收集它并在自己的时间释放使用过的 memory。
You may find that the memory used by fb
is not freed immediately - this is perfectly normal and to be expected.您可能会发现
fb
使用的 memory 没有立即释放 - 这是完全正常的,也是意料之中的。 There are ways of forcing the garbage collector into doing "its thing" when you want it to, but doing this is very bad practice .有一些方法可以强迫垃圾收集器在你想要的时候做“它的事情”,但这样做是非常糟糕的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.