演员表<int>到列表<字符串>在 .NET 2.0 中 [英] Cast List&lt;int&gt; to List&lt;string&gt; in .NET 2.0

查看:21
本文介绍了演员表<int>到列表<字符串>在 .NET 2.0 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能以某种方式将 List 转换为 List 吗?

Can you cast a List<int> to List<string> somehow?

我知道我可以循环遍历和 .ToString() 的东西,但是转换会很棒.

I know I could loop through and .ToString() the thing, but a cast would be awesome.

我使用的是 C# 2.0(所以没有 LINQ).

I'm in C# 2.0 (so no LINQ).

推荐答案

.NET 2.0 具有 ConvertAll 方法,您可以在其中传入转换器函数:

.NET 2.0 has the ConvertAll method where you can pass in a converter function:

List<int>    l1 = new List<int>(new int[] { 1, 2, 3 } );
List<string> l2 = l1.ConvertAll<string>(delegate(int i) { return i.ToString(); });

这篇关于演员表<int>到列表<字符串>在 .NET 2.0 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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