C# 将 List<string> 中的值分配给字符串变量

标签 c#

在 Java 中,我可以将 Vector 中的值赋给 String 变量。

String str = vector1.elementAt(0).toString();

如何在 C# 中使用 List 做同样的事情?

谢谢。

最佳答案

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

假设

List<string> yourList;

然后所有下面的将元素放在位置 index在字符串变量中:

  1. string s = yourList[index];
  2. string s = yourList.ToArray()[index];
  3. string s = yourList.ElementAt(index);

在以上所有内容中,index必须在 0 - (yourList.Length-1) 范围内因为 C# 中的数组索引是从零开始的。

另一方面,虽然看起来一样,但甚至无法编译:

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

.Take()在这种情况下不返回 string但是一个IEnumerable<string>这仍然是一个集合。

关于C# 将 List<string> 中的值分配给字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11932257/

相关文章:

c# - 如何在 WebBrowser 控件 C# 中查找特定单词

c# - 多个提交按钮 asp.net mvc 5 不工作

C++ 库的 C# 绑定(bind)

c# - ASP.NET Web API 中的参数绑定(bind)

c# - 选择 SqlCommand 返回字符

c# - Firefox 中的 Google map Api 共享位置问题

C#:部分方法和操作,未实现的主体

c# - SQL Server 查询与 varbinary 类型列不匹配

c# - 在 WCF 的代码中将 IncludeExceptionDetailInFaults 设置为 true

c# - 启用 ActiveX 控件以在不在 System.Windows.Forms.Application 中运行的情况下引发事件