有一个。每个()(或.ForEach())的迭代器中的.Net标准库?有一个、器中、迭代、标准

2023-09-04 00:39:27 作者:挚爱

可能重复:    LINQ等价的foreach的IEnumerable&LT的; T>

我不知道是否有对IEnumerable的一个方法,像下面的每个()在NET库

I'm wondering whether there is a method for IEnumerable like the following .Each() in the .Net library

var intArray = new [] {1, 2, 3, 4};
intArrary.Each(Console.WriteLine);

我知道我可以使用的foreach 循环或随便写一个这样的扩展方法:

I know I can use a foreach loop or easily write an extension method like this:

public static class EnumerableExtensions
{
    public static void Each<T>(this IEnumerable<T> enumberable, Action<T> action)
    {
        foreach (var item in enumberable)
        {
            action(item);
        }
    }
}

但我希望不要创建自己的方法来搞砸了code如果已经在图书馆这样的扩展方法。而类似。每()(少数overloadings,可采取的条件额外PARAMS)在很大程度上需要由程序员,应该已经是一个。我是正确?

But I'm hoping not to create my own method to mess up code if there is already such an extension method in the library. And something like .Each() (with a few overloadings which can take conditions as extra params) is heavily needed by programmers, and there should already be one. Am I correct?

更新

Ruby开发人员可以将其识别为每个()迭代器。而这正是我希望在C#。也许C#中可以有更多的迭代方法,如那些红宝石。

Ruby developers may recognize it as a .each() iterator. And that's what I hope to have in C#. Maybe C# can have more iterator methods like those in Ruby.

推荐答案

的IEnumerable&LT; T&GT; 。 LINQ的团队是反对按这个职位由埃里克利珀:: 的http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

As others have said there is none built in on IEnumerable<T>. The Linq team was against it as per this post by Eric Lippert:: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

有一个静态的方法对 Array.ForEach 名单,其中,T&GT; 有一个实例方法。也有在PLINQ的foreach语句一样,但被警告,他们并行工作,并可能导致非常糟糕的表现了极其简单的动作。 这是在PLINQ这样一种方法: http://msdn.microsoft.com/en -us /库/ dd383744.aspx

There is a static method on Array.ForEach and List<T> has an instance method. There is also in PLINQ foreach like statements, but be warned that they work in parallel and can lead to very bad performance for extremely simple actions. Here is one such method in PLINQ: http://msdn.microsoft.com/en-us/library/dd383744.aspx

这里是一般的PLINQ指南:的http:// MSDN。 microsoft.com/en-us/library/dd460688.aspx

And here is a guide on PLINQ in general: http://msdn.microsoft.com/en-us/library/dd460688.aspx

虽然我不能找到确切的文章,如果您在ParrallelEnumerable部分它给人的警告和提示,如何提高code。使用并行的性能闲逛

While I can't find the exact article if you poke around in the ParrallelEnumerable section it gives warnings and tips as to how to improve the performance of using parallelism in code

如果你想要的话,我建议创建2个版本,一个包含索引,一个没有。这是非常有用的,可以节省一个select语句获取索引。 例如,

If you want it, I suggest creating 2 versions, one that include indexer and one without. This can be quite useful and can save a select statement to acquire the index. e.g.

public static void ForEach<T>(IEnumerable<T> enumerable,Action<T> action)
{
    foreach(var item in enumerable) action(item);
}

public static void ForEach<T>(IEnumerable<T> enumerable,Action<T,int> action)
{
    int index = 0;
    foreach(var item in enumerable) action(item,index++);
}

我也想包括参数验证,因为这些都是公开的方法。

I'd also include argument validation as these are public methods.

 
精彩推荐
图片推荐