在.Net或任何垃圾收集语言中,您只需释放您对该对象持有的所有引用,垃圾收集器最终将收集它Ex:
int[] arr = new int[20];
....
// when no longer needed set all references to null
arr = null;
// also creating a new object will release the old one automatically if there are no more references to it
arr = new int[40]; // old array will be garbage collected
Run Code Online (Sandbox Code Playgroud)
另请注意,每次都不需要这样做,只有当您明确要释放对象而不释放它的父对象或者引用是静态字段时才需要这样做.对于本地(方法)变量,类或静态字段的字段也不需要释放对象.