简体   繁体   中英

Local variable not initializing in C#

I have a static function that initializes a list of tiles:

private static bool MyFunction(SomeObj obj) 
{
    List<Tile> tiles = obj.Tile.GetTilesBetween(otherTile);
    
    // do stuff
}

Later in MyFunction(), I initialize a collection using LINQ:

{
    // we did stuff

    List<Commands> commands = CommandsOccuringInTimespan(obj.timeElapsed);
    IEnumerable<Commands> match = commands.Where(c => tiles.Contains(c.End));

    // do more stuff
}

However, the presence of these lines, specifically the initialization of match , is causing tiles to either not initialize or to initialize but not be available in the Debugger. I can't understand why this would be happening.

I can seem to make it work if I make a copy of tiles and use that when initializing match:

    // we did stuff

    List<Commands> commands = CommandsOccuringInTimespan(obj.timeElapsed);
    List<Tile> copyOfTiles = new List<Tile>(tiles);
    IEnumerable<Commands> match = commands.Where(c => copyOfTiles.Contains(c.End));

    // do more stuff
}

C# 7 (I think; this is a Unity3D app so I believe that's the current supported version).

尝试将 .ToList() 方法添加到磁贴:

List<Tile> tiles = obj.Tile.GetTilesBetween(otherTile).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM