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 :: wpf c# select folder path 
Csharp :: ienumerable count 
Csharp :: c# messagebox result 
Csharp :: add text to combobox c# 
Csharp :: unity cap fps 
Csharp :: how to make dictionary c# 
Csharp :: can you have multiple statement in a case c# 
Csharp :: unity actions 
Csharp :: get attribute value of xml element c# 
Csharp :: c# Get all class by namespace 
Csharp :: function on animation exit unity 
Csharp :: how to create a random vector2 in unity 
Csharp :: increment operator c# 
Csharp :: c# bitmap to array byte 
Csharp :: c# cancellationtoken example 
Csharp :: ef rollback migration 
Csharp :: how to get specific length of row in matrix c# 
Csharp :: unity rewarded ads 
Csharp :: Replaced OS is obselete 
Csharp :: c# clear all textboxes 
Csharp :: switch expression c# 
Csharp :: c# string contains 
Csharp :: c# foreach object in array json 
Csharp :: find how many digits a number has 
Csharp :: how to deserialize string array in c# 
Csharp :: c# random 
Csharp :: convert html to pdf c# 
Csharp :: unity c# change animation 
Csharp :: priority queue c# 
Csharp :: how to create a variable in c# 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =