c# - 在 C# 中将 List<string> 转换为 List<int>

标签 c#

我想转换一个 List<string>List<int> .

这是我的代码:

void Convert(List<string> stringList)
{
    List<int> intList = new List<int>();  
    for (int i = 0; i < stringList.Count; i++) 
    {  
        intList.Add(int.Parse(stringList[i]));  
    }
)

最佳答案

您可以使用 List<T>.ConvertAll<TOutput>(...) 而不是使用 LINQ

List<int> intList = stringList.ConvertAll(int.Parse);

关于c# - 在 C# 中将 List<string> 转换为 List<int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740383/

相关文章:

c# - asp.net 如何检查一个项目是否存在于列表框中?

c# - unity3d 字典的无效转换异常

c# - AutoGenerateBindingRedirects 不适用于单元测试

c# - 集成测试 .NET Core 3 Worker

c# - 如何使用 Entity Framework 将现有实体添加为新实体

c# - 更改 slider 缩略图工具提示的文本/数据格式

c# - ASP.NET 中的单选按钮

c# - 没有接口(interface)的 CaSTLe Windsor 拦截器?

c# - 在 C# 中将 2 个字节转换为 Short

c# - 使用 Swashbuckle Swagger 上传文件