数组列表列出< T>? [英] Arraylist to list<t>?

查看:161
本文介绍了数组列表列出< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以转换/转储一个ArrayList到一个列表?我使用ArrayList的,因为我使用的是ASP.NET配置文件功能,它看上去像一个痛苦的列表存储在配置文件中。

How can I convert/dump an arraylist into a list? I'm using arraylist because I'm using the ASP.NET profiles feature and it looked like a pain to store List in profiles.

请注意: 另一种选择是将包裹列到自己的类和ArrayList的废除。

Note: The other option would be to wrap the List into an own class and do away with ArrayList.

<一个href="http://www.i$p$pferjim.com/site/2009/04/storing-generics-in-asp-net-profile-object/">http://www.i$p$pferjim.com/site/2009/04/storing-generics-in-asp-net-profile-object/

推荐答案

要转换的的ArrayList 全类型的对象 T <最简单的方法/ code>会是这样(假设你使用.NET 3.5或更高版本):

The easiest way to convert an ArrayList full of objects of type T would be this (assuming you're using .NET 3.5 or greater):

List<T> list = arrayList.Cast<T>().ToList();

如果您使用的是3.0或更早版本,你必须自己循环:

If you're using 3.0 or earlier, you'll have to loop yourself:

List<T> list = new List<T>(arrayList.Count);

foreach(T item in arrayList) list.Add(item);

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

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