处理类实例 [英] Dispose of a class instance

查看:62
本文介绍了处理类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在字典中创建的类。当我删除字典时,我怎样才能删除该类的实例?我不能等待垃圾收集器为我清理它。我可以创建几个完全相同的类的多个实例,我想确保一旦我完成它们就删除它们。类似的东西。



<前lang =cs> 测试
{
public string 名称;
public int 计数;
}


静态 void Main(< span class =code-keyword> string [] args)
{
Dictionary< int,Test> DictTags = new Dictionary< int,Test>();

for int X = 0 ; X < = 10 ; X ++)
{
DictTags.Add(X, new Test());
}

DictTags.Remove( 2 );

解决方案

您不了解GC的工作原理。这里有很好的解释: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ]。



注意概念无法访问的内存;这是关键: http://en.wikipedia.org/wiki/Unreachable_memory [ ^ ]。



请注意这个概念并不像它想象的那样微不足道。考虑一个简单的例子:三个对象在循环中相互引用。人们可能会认为他们会在记忆中保持彼此,防止破坏和回收记忆。这不是真的 - 如果没有其他引用,GC将检测到这种情况并销毁对象;这很容易检查。



不要混淆处置的概念,特别是在接口<$ c $中理解它C> System.IDisposable的。它用于以明确控制的方式实施任何清理行动;这种清理可以是任何东西;它与对象的破坏和回收托管内存无关。尽管此接口通常用于非托管内存释放,但它也可用于许多其他事情,无论如何。请参阅:

https:/ /msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



另请参阅对您问题的评论。



-SA


I have a class that gets created within a dictionary. When I remove the dictionary how can I also remove the instance of the class? I can't wait for the garbage collector to clean it up for me. I could be creating multiple instances of the class that are virtually identical and I want to make sure I delete the them as soon as I'm done with them. Something similar to this.

class Test
{
  public string Name;
  public int Count;
}


static void Main(string[] args)
{
  Dictionary<int, Test> DictTags = new Dictionary<int, Test>();

  for (int X = 0; X <= 10; X++)
  {
    DictTags.Add(X, new Test());
  }

  DictTags.Remove(2);

解决方案

You don't understand how GC works. This is well explained here: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Pay attention for the concept unreachable memory; this is the key: http://en.wikipedia.org/wiki/Unreachable_memory[^].

Note that this concept is not as trivial as it may think. Consider one simple example: three objects reference each other in cycle. One may think that they will "hold" each other in memory, preventing destruction and reclaiming memory. This is not true — if there are no other references, GC will detect this situation and destroy objects; this is easy to check up.

Don't mix up the concept of "disposal", as, in particular, it is understood in the interface System.IDisposable. It is used to implement any clean-up actions in explicitly controlled manner; this clean-up can be anything; it does not related to destruction of object and reclaiming managed memory. Even though this interface is often used for unmanaged memory deallocation, it is also used for many other things, no matter what. Please see:
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

See also the comments to your question.

—SA


这篇关于处理类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆