C#从列表中赋值<;字符串>;添加到字符串变量

C#从列表中赋值<;字符串>;添加到字符串变量,c#,C#,在Java中,我可以将Vector中的一个值赋给字符串变量 String str = vector1.elementAt(0).toString(); 如何在C#with List中执行相同的操作 谢谢。列表=。。。 List<string> list = ... ... string str = list[0]; ... ... 字符串str=list[0]; ... 您可以将索引与列表一起使用 List<string> list = new List<str

在Java中,我可以将Vector中的一个值赋给字符串变量

String str = vector1.elementAt(0).toString();
如何在C#with List中执行相同的操作

谢谢。

列表=。。。
List<string> list = ...
...
string str = list[0];
...
... 字符串str=list[0]; ...
您可以将索引与列表一起使用

List<string> list = new List<string>();
string str = list[0];
List List=新列表();
字符串str=list[0];
//创建字符串列表
List lst=新列表();
...
//字符串由值填充,i是int
字符串ithValue=lst[i];

字符串str=vector1[0].ToString()

有很多方法可以做到这一点:

假定

List<string> yourList;
列出你的清单;
然后,以下所有操作都会将元素放置在字符串变量中的位置
index

  • string s=yourList[index]
  • string s=yourList.ToArray()[index]
  • string s=yourList.ElementAt(索引)
  • 在上述所有情况下,
    index
    必须在
    0-(yourList.Length-1)
    范围内,因为C#中的数组索引是以零为基础的

    另一方面,虽然看起来是一样的,但它甚至不会编译:

  • string s=youList.Skip(index.Take)(1)

  • .Take()
    在这种情况下,不会返回
    字符串,而是返回一个仍然是集合的
    IEnumerable

    string s=myList[0]。ToString()
    如果它不是字符串,
    string s=myList[0]
    如果
    myList=new List()
    List<string> yourList;