Are Linq operators with a generic state parameter considered? #7672
-
I am a big fan of the declarative nature of Linq statements, but it is hard to use without causing (accidental) heap allocations. With an added state parameter a lot of those allocations can be avoided because the delegate instance can be cached. In this example, the using System;
using System.Collections.Generic;
using System.Linq;
public class C
{
public int Threshold;
public bool M(List<int> list)
{
return list.Any(i => i > 42);
}
public bool N(List<int> list)
{
return list.Any(i => i > Threshold);
}
public bool O(List<int> list)
{
return list.Any(this, (i, s) => i > s.Threshold);
}
}
public static class IEnumerableExt
{
public static bool Any<T, TState>(this IEnumerable<T> e, TState state, Func<T, TState, bool> predicate)
{
foreach (var item in e)
{
if (predicate.Invoke(item, state)) return true;
}
return false;
}
} Are these extensions to the Linq operators considered? Or are there other improvements to the allocations incurred by Linq in the works? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Are you asking for new extension methods, or new query syntax? If the former, you'll want to open an API proposal in the dotnet/runtime repo. |
Beta Was this translation helpful? Give feedback.
Are you asking for new extension methods, or new query syntax? If the former, you'll want to open an API proposal in the dotnet/runtime repo.