F#List.map在C#中的等效值?
C#中是否有与F#的List.map函数等价的函数?i、 e.对列表中的每个元素应用一个函数,并返回一个包含结果的新列表 比如:F#List.map在C#中的等效值?,c#,.net,linq,f#,C#,.net,Linq,F#,C#中是否有与F#的List.map函数等价的函数?i、 e.对列表中的每个元素应用一个函数,并返回一个包含结果的新列表 比如: public static IEnumerable<TResult> Map<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> funky) { foreach (TSource eleme
public static IEnumerable<TResult> Map<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> funky)
{
foreach (TSource element in source)
yield return funky.Invoke(element);
}
公共静态IEnumerable映射(此IEnumerable源代码,Func funky)
{
foreach(源中的TSource元素)
返回funky.Invoke(元素);
}
是否已经有内置的方式,或者我应该只编写自定义扩展?这是LINQ的
选择
-即
var newSequence = originalSequence.Select(x => {translation});
或
是内置功能:
public List<TOutput> ConvertAll<TOutput>(
Converter<T, TOutput> converter
)
public-List-ConvertAll(
转换器
)
从.NET 2.0版开始提供
MSDN代码示例:
using System;
using System.Drawing;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<PointF> lpf = new List<PointF>();
lpf.Add(new PointF(27.8F, 32.62F));
lpf.Add(new PointF(99.3F, 147.273F));
lpf.Add(new PointF(7.5F, 1412.2F));
Console.WriteLine();
foreach( PointF p in lpf )
{
Console.WriteLine(p);
}
List<Point> lp = lpf.ConvertAll(
new Converter<PointF, Point>(PointFToPoint));
Console.WriteLine();
foreach( Point p in lp )
{
Console.WriteLine(p);
}
}
public static Point PointFToPoint(PointF pf)
{
return new Point(((int) pf.X), ((int) pf.Y));
}
}
/* This code example produces the following output:
{X=27.8, Y=32.62}
{X=99.3, Y=147.273}
{X=7.5, Y=1412.2}
{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
*/
使用系统;
使用系统图;
使用System.Collections.Generic;
公开课范例
{
公共静态void Main()
{
List lpf=新列表();
lpf.Add(新的PointF(27.8F,32.62F));
lpf.Add(新的PointF(99.3F,147.273F));
lpf.Add(新的PointF(7.5F,1412.2F));
Console.WriteLine();
foreach(lpf中的p点)
{
控制台写入线(p);
}
List lp=lpf.ConvertAll(
新转换器(PointFToPoint));
Console.WriteLine();
foreach(lp中的p点)
{
控制台写入线(p);
}
}
公共静态点PointF点(PointF pf)
{
返回新点(((int)pf.X),((int)pf.Y));
}
}
/*此代码示例生成以下输出:
{X=27.8,Y=32.62}
{X=99.3,Y=147.273}
{X=7.5,Y=1412.2}
{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
*/
using System;
using System.Drawing;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<PointF> lpf = new List<PointF>();
lpf.Add(new PointF(27.8F, 32.62F));
lpf.Add(new PointF(99.3F, 147.273F));
lpf.Add(new PointF(7.5F, 1412.2F));
Console.WriteLine();
foreach( PointF p in lpf )
{
Console.WriteLine(p);
}
List<Point> lp = lpf.ConvertAll(
new Converter<PointF, Point>(PointFToPoint));
Console.WriteLine();
foreach( Point p in lp )
{
Console.WriteLine(p);
}
}
public static Point PointFToPoint(PointF pf)
{
return new Point(((int) pf.X), ((int) pf.Y));
}
}
/* This code example produces the following output:
{X=27.8, Y=32.62}
{X=99.3, Y=147.273}
{X=7.5, Y=1412.2}
{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
*/