如何在C#中获取列表中新添加项的索引? [英] How do I get the index of a newly added item in a list in C#?

查看:71
本文介绍了如何在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));

这篇关于如何在C#中获取列表中新添加项的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆