Подтвердить что ты не робот

Тип или имя пространства имен 'T' не найдено

У меня есть следующий код, который я компилирую в проекте .NET 4.0

namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  

        }  
    }  

    public static class Utility  
    {  
        public static IEnumerable<T> Filter1(this IEnumerable<T> input, Func<T, bool> predicate)  
        {  
            foreach (var item in input)  
            {  
                if (predicate(item))  
                {  
                    yield return item;  
                }  
            }  
        }  
    }  
}  

но получая следующие ошибки. У меня System.dll уже включен в качестве ссылок по умолчанию. Что я могу делать неправильно?

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?) 
4b9b3361

Ответ 1

public static class Utility 
{  
    public static IEnumerable<T> Filter1<T>( // Type argument on the function
       this IEnumerable<T> input, Func<T, bool> predicate)  
    {  

Если вам не нравится, если его метод расширения или нет, вы можете добавить общий класс к классу. Я предполагаю, что вы хотите использовать метод расширения.

public static class Utility<T> // Type argument on class
{  
    public static IEnumerable<T> Filter1( // No longer an extension method
       IEnumerable<T> input, Func<T, bool> predicate)  
    {  

Ответ 2

Вы должны поместить аргумент типа непосредственно в функцию.

public static IEnumerable<T> Filter1<T>(...)

Ответ 3

Вам нужно объявить T, который возникает после имени метода или имени класса. Измените объявление метода:

public static IEnumerable<T> 
    Filter1<T>(this IEnumerable<T> input, Func<T, bool> predicate) 

Ответ 5

У меня была та же ошибка, но требуемое решение было немного другим. Мне нужно было изменить это:

public static void AllItemsSatisy(this CollectionAssert collectionAssert, ICollection<T> collection, Predicate<T> predicate) 
{ ... }

К этому:

public static void AllItemsSatisy<T>(this CollectionAssert collectionAssert, ICollection<T> collection, Predicate<T> predicate) 
{ ... }