I have following code that I am compiling in a .NET 4.0 project
public static class Ext
{
public static IEnumerable<T> Where(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (predicate == null)
{
throw new ArgumentNullException("predicate");
}
return WhereIterator(source, predicate);
}
private static IEnumerable<T> WhereIterator(IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (T current in source)
{
if (predicate(current))
{
yield return current;
}
}
}
}
but getting following errors. I have System.dll already included as default in references. What I may be doing wrong?
Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
Error 2 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
Error 3 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
Try:
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
And
private static IEnumerable<T> WhereIterator<T>(IEnumerable<T> source, Func<T, bool> predicate)
In short, you're missing the generic T declaration (which all other T's are inferred from) in the method signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With