转换字符串数组列出<串GT; [英] Convert array of strings to List<string>

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

问题描述

我见过的这个使用 .ToList()的数组类型,这似乎是唯一可用的in净3.5+ 。我对那些不能在这个时候进行升级ASP.NET项目的.NET Framework 2.0的工作,所以我想知道:有另一种解决方案?一个比通过数组循环和增加每个元素名单更优雅(这是没有问题的,我只是想知道是否有出于学习目的更好的解决方案)?

I've seen examples of this done using .ToList() on array types, this seems to be available only in .Net 3.5+. I'm working with .NET Framework 2.0 on an ASP.NET project that can't be upgraded at this time, so I was wondering: is there another solution? One that is more elegant than looping through the array and adding each element to this List (which is no problem; I'm just wondering if there is a better solution for learning purposes)?

string[] arr = { "Alpha", "Beta", "Gamma" };

List<string> openItems = new List<string>();

foreach (string arrItem in arr)
{
    openItems.Add(arrItem);
}

如果我必须这样做,这样一来,有没有办法来释放从内存中挥之不去阵列后,我将其复制到我的名单?

If I have to do it this way, is there a way to deallocate the lingering array from memory after I copy it into my list?

推荐答案

只需使用的 名单,LT的这个构造; T&GT; 。它接受任何的IEnumerable&LT; T&GT; 作为参数

Just use this constructor of List<T>. It accepts any IEnumerable<T> as an argument.

string[] arr = ...
List<string> list = new List<string>(arr);

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

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