Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

IEnumerable ForEach

public static class Enumerables
{
    public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action)
    {
        foreach (T item in @this)
        {
            action(item);
        }
    }
}

class Program
{
    private static void NoOp(int value) {}

    static void Main(string[] args)
    {
        var list = Enumerable.Range(0, 10).ToList();
        for (int i = 0; i < 1000000; i++)
        {
            // WithLinq(list);
            // WithoutLinqNoGood(list);
            WithoutLinq(list);
        }
    }

    private static void WithoutLinq(List<int> list)
    {
        foreach (var item in list)
        {
            NoOp(item);
        }
    }

    private static void WithLinq(IEnumerable<int> list) => list.ForEach(NoOp);

    private static void WithoutLinqNoGood(IEnumerable<int> enumerable)
    {
        foreach (var item in enumerable)
        {
            NoOp(item);
        }
    }
}
Comment

ienumerable for each

items.ToList().ForEach(i => i.DoStuff());
// OR
foreach(T item in enumeration) {
	// Actions
}
Comment

foreach for IEnumerable

public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
    foreach(T item in enumeration)
    {
        action(item);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: How to set default page asp.net MVC 
Csharp :: c# read string 
Csharp :: SQLite Parameters 
Csharp :: Generic Stack 
Csharp :: long string c# 
Csharp :: remove all values from list c# 
Csharp :: unity play animation on click 
Csharp :: c# new object 
Csharp :: C# Console font 
Csharp :: c# read file stream 
Csharp :: c# yield return ienumerable 
Csharp :: crud operation in asp.net 
Csharp :: Default property value in C# 
Csharp :: linq syntax 
Csharp :: unity animation length 
Csharp :: binary tree c# 
Csharp :: c# get enum name from value 
Csharp :: C3 compare hour 
Csharp :: check .net installing 
Csharp :: dotnet core clickable row 
Csharp :: dynamics 365 create record c# 
Csharp :: how to make font factory text to bold in c# 
Csharp :: asp.net core 6 get current culture in controller 
Csharp :: real world example of sinleton design pattern 
Csharp :: entity framework dynamic search solution 1 
Csharp :: .net core 3 entity framework constraint code first image field 
Csharp :: overloading constructors c# 
Csharp :: générer un nombre aléatoire en c# 
Csharp :: OIUJHKJHSKAL::KSAJ 
Csharp :: How to execute a script after the c# function executed 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =