Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the index of a newly added item in a list in C#?

Tags:

c#

list

indexing

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?

Example code:

MapTiles.Add(new Class1(num, x * 32 + cameraX, y * 32 + cameraY));
like image 834
Marcus Avatar asked Dec 02 '22 01:12

Marcus


1 Answers

MapTiles.Count will give you the index of next item that will be added to the list

Something like:

Console.WriteLine("Adding " + MapTiles.Count + "th item to MapTiles List");
MapTiles.Add(new Class1(num, x * 32 + cameraX, y * 32 + cameraY));
like image 72
Chandu Avatar answered Dec 19 '22 18:12

Chandu