转换列表<串GT;列出< INT>在C#中 [英] Convert List<string> to List<int> in C#

查看:194
本文介绍了转换列表<串GT;列出< INT>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个列表<串> 列表< INT方式>



下面是我的代码:

 无效转换(列表<串>的StringList)
{
名单,LT; INT> intList中=新名单< INT>();
的for(int i = 0; I< stringList.Count;我++)
{
intList.Add(int.Parse(StringList的[I]));
}


解决方案

相反使用LINQ,你可以使用 列表< T> .ConvertAll< TOutput>(.. 。)

 列表< INT> intList中= stringList.ConvertAll(S => Int32.Parse(S)); 


I want to convert a List<string> to a List<int>.

Here is my code:

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]));  
    }
)

解决方案

Instead of using LINQ you can use List<T>.ConvertAll<TOutput>(...)

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

这篇关于转换列表&LT;串GT;列出&LT; INT&GT;在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆