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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With