如何在c#中删除一个数组?

如何在c#中删除一个数组?

问题描述:

如果这是一个明显的问题,我很抱歉,但无论是 Google 还是此处的搜索都没有让我找到答案.

I'm sorry if this is an obvious question but neither Google or a search here led me to an answer.

有没有办法完全删除数组?

Is there a way to remove an array entirely?

我想要int[] array = new int[5]

说你打电话:

 void Foo(){
     int[] a = new int[5];
 }

在 C# 中,无法取消定义变量 a.这意味着即使您将 a 设置为 null,a 也会在 Foo 中定义.但是,在 Foo 的末尾 a 将超出范围.这意味着没有代码可以引用它,垃圾收集器将在下次运行时为您释放内存,这可能不会持续很长时间.

In C# there is no way to undefine the variable a. That means a will be defined in Foo even if you set a to null. However, at the end of Foo a will fall out of scope. That means no code can reference it, and the garbage collector will take care of freeing the memory for you the next time it runs, which might not be for a long time.