Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get list item by id

I have a list:

List<string> theList = new List<string>;

There are a few elements in the List. And now I want to get an item by the index. e.g. I want to get element number 4. How can I do it?

like image 567
tux007 Avatar asked Dec 05 '22 12:12

tux007


1 Answers

Just use the indexer

string item = theList[3];

Note that indexes in C# are 0 based. So if you want the 4th element in the list you need to use index 3. If you want the 5th element you would use index 4. It's unclear from your question which you intended

The indexer is a common feature on .Net collection types. For lists it's generally index based, for maps it's key based. The documentation for the type in question will tell you which and if it's available. The indexer member will be listed though as the property named Item

http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx

like image 140
JaredPar Avatar answered Dec 22 '22 07:12

JaredPar