如果我替换数组中对 class 实例的唯一引用,它会破坏原始实例吗?

If I replace the only reference to an instance of a class in an array, does it destroy the original instance?

我正在尝试通过实例化与每个图块关联的 class 并将它们存储在数组中来创建基于网格的地图。 GenerateBattlefieldTiles() 方法只是生成一个完整的通用图块地图,它将被后面的方法替换。现在我正在研究一个路径生成器,我想知道替换数组中的一个实例是否会破坏该实例,因为数组是对所述实例的唯一引用,或者我是否必须在替换它之前销毁该实例。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattlefieldManager : MonoBehaviour
{
    public int pathWaypointCount;
    public List<PathTile> paths;
    public BattlefieldTile[,] battlefieldTiles;
    public int xSize;
    public int ySize;
    public int seed;
    public enum TileType { Path, Obstacle, Generic, Buildable }

    // Start is called before the first frame update
    void Start()
    {

    }

    void GenerateBattlefieldTiles()
    {
        battlefieldTiles = new BattlefieldTile[xSize, ySize];
        for (int y = 0; y < battlefieldTiles.GetLength(1); y++)
        {
            for (int x = 0; x < battlefieldTiles.GetLength(0); x++)
            {
                Vector2 tilePosition = new Vector2(x - xSize / 2, ySize / 2 - y);
                battlefieldTiles[x, y] = new BattlefieldTile(TileType.Generic, tilePosition, new Vector2(x, y));
            }
        }
    }

    void GenerateWaypoints()
    {
        Random.InitState(seed);
        int entryYValue = Random.Range(1, ySize - 1);
        int exitYValue = Random.Range(1, ySize - 1);
        PathTile entryTile = new PathTile(TileType.Path, new Vector2(-xSize / 2, battlefieldTiles.GetLength(1) / 2 - entryYValue), new Vector2(0, entryYValue));
        paths.Add(entryTile);
        battlefieldTiles[0, entryYValue] = entryTile;
        PathTile exitTile = new PathTile(TileType.Path, new Vector2(xSize / 2, battlefieldTiles.GetLength(1) / 2 - exitYValue), new Vector2(xSize, exitYValue));
        battlefieldTiles[xSize, exitYValue] = exitTile;

        while (paths.Count < pathWaypointCount)
        { 
            Vector2 newWaypoint = new Vector2(Random.Range(1, xSize - 1), Random.Range(1, ySize - 1));
            int i = 0;
            foreach (PathTile path in paths)
            {
                if (newWaypoint == path.arrayRef)
                {
                    return;
                }
                if (Mathf.Abs(newWaypoint.x - path.arrayRef.x) == 1 && Mathf.Abs(newWaypoint.y - path.arrayRef.y) == 1)
                {
                    i++;
                    if (i >= 2)
                    {
                        return
                    }
                }
                PathTile pathTile = new PathTile(TileType.Path, new Vector2(newWaypoint.x - xSize / 2, ySize / 2 - newWaypoint.y), newWaypoint);
                paths.Add(pathTile);
                battlefieldTiles[Mathf.RoundToInt(newWaypoint.x), Mathf.RoundToInt(newWaypoint.y)] = pathTile;
            }
        }
    }
}

简而言之,是的,它会被摧毁,不需要你采取任何其他行动。终于。

较长的答案是实例,或者更具体地说,您引用的对象的内存地址,将可用于垃圾回收。我不知道废弃的内存需要多长时间才能被标记为收集。

我确实知道,如果您有意将对象设置为 null,您可以手动调用垃圾收集器来立即清理它。但是不要这样做,因为您只能调用一个完整的集合,而不能调用一个专注于这个特定对象的集合。

作为您特定情况之外的一般说明,如果您有一个对象使用其他资源并且您希望更立即释放这些资源,例如网络连接或文件锁,您应该实施 IDisposable.在 Dispose() 方法中,您将清除所有这些引用。然后,与其让该对象超出范围,不如对其调用 dispose 方法来立即清理这些资源。否则,它们将保持打开状态,直到垃圾收集按照自己的时间表进行处理。