Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension method. The type or namespace name 'T' could not be found

Tags:

c#

.net

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?) 
like image 405
Dima Babich Avatar asked Jul 25 '13 09:07

Dima Babich


1 Answers

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.

like image 198
haim770 Avatar answered Oct 14 '22 23:10

haim770