Does anyone know why C# is unable to infer the generic type arguments in this example?
var i = "1 2 3 dogs 4 5".Split(' ').SelectWhere(int.TryParse);
Where:
public delegate bool TryConverter<TIn, TOut>(TIn input, out TOut output);
public static IEnumerable<TOut> SelectWhere<TIn, TOut>(this IEnumerable<TIn> source, TryConverter<TIn, TOut> converter)
{
foreach (TIn input in source)
{
if (converter(input, out TOut output))
yield return output;
}
}
I have encountered difficulties with type inference and custom delegate types in the past (but, AFAIK, because of the out parameter I'm unable to use my typical fallback to something like Func<TIn, TOut, bool>
in this case).
This is obviously not a show-stopper—I can simply specify the type parameters or even do something like new TryConverter<string, int>(int.TryParse)
. I'm more curious as to why C# can't/won't infer in situations like these.