In Java, I can assign a value from Vector to a String variable.
String str = vector1.elementAt(0).toString();
How can I do the same thing in C# with List?
Thanks.
There are many ways to do this:
Assuming
List<string> yourList;
Then all of the following would put the element in position index inside a string variable:
string s = yourList[index];string s = yourList.ToArray()[index];string s = yourList.ElementAt(index);In all of the above, index must fall within the range 0 - (yourList.Length-1) since array indexing in C# is zero-based.
This, on the other hand, while it would seem the same, won't even compile:
string s = youList.Skip(index).Take(1);.Take() in this case doesn't return a string but a IEnumerable<string> which is still a collection.
string s = myList[0].ToString()if it isn't a string,string s = myList[0]ifmyList = new List<string>();.