Is there a function that would allow you to add an element to a List<T> and get the index as int back? List<T>.Add() is a void and does not return a value.
List.Count-1 is a solution when your are not working with threads. My list is a static member and can be accessed with multiple thread at a time and using the count-1 value is totally thread unsafe and could easily lead to wrong results.
The index will be used for specific treatment to each element.
Thank you!
List’s methods are not thread safe alone; if you just call
List.Addrepeatedly from several threads you could very well run into problems. You need to use some sort of synchronization technique if you’re going to use aListin the first place, so you might as well include a call toList.Countinside of that critical section.Your other option would be to use a collection that is designed to be used by multiple threads, such as those in
System.Collections.Concurrent.