List<string> a=new List<string>();</P> <P>a.Add("1");</P> <P>a.Add("2");</P> <P>现在想对list中的每个元素除2,使用foreach好像不会返回值啊,</P> <P>大概感觉是使用IEnumerator写个扩展方法,但是动手又不会写了,高手帮忙
2 回答
杨魅力
TA贡献1811条经验 获得超6个赞
public static IEnumerable<T> Map<T, U>(this IEnumerable<U> source, Func<U,T> mapping) { foreach(var s in source) yield return mapping(s); } List<string> a=new List<string>(); a.Add("1"); a.Add("2"); var b = a.Map(x=> int.Parse(x) /2).ToList();
- 2 回答
- 0 关注
- 414 浏览
添加回答
举报
0/150
提交
取消