如何在C#中获取列表中新添加项的索引?
问题描述:
当我将一个项目(一个类的实例)添加到列表中时,我需要知道新项目的索引。是否可以使用任何函数?
When I add an item (an instance of a class) to a list, I need to know the index of the new item. Is it possible with any function?
示例代码:
MapTiles.Add(new Class1(num, x * 32 + cameraX, y * 32 + cameraY));
答
MapTiles.Count
将为您提供将添加到列表中的下一项的索引
MapTiles.Count
will give you the index of next item that will be added to the list
类似于:
Console.WriteLine("Adding " + MapTiles.Count + "th item to MapTiles List");
MapTiles.Add(new Class1(num, x * 32 + cameraX, y * 32 + cameraY));