无法找到类型或名称空间名称“T” [英] The type or namespace name 'T' could not be found

查看:185
本文介绍了无法找到类型或名称空间名称“T”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 命名空间ConsoleApplication1 
{
class Program
{
static void Main(string [] args)
{

}
}

public静态类实用程序
{
public static IEnumerable< T> Filter1(此IEnumerable< T>输入,Func< T,bool>谓词)
{
foreach(输入中的var项)
{
if(predicate(item))
{
收益退货项目;
}
}
}
}
}

但正在追踪错误。 System.dll已经包含在引用中作为默认值。我可能做错了什么?

 错误1无法找到类型或命名空间名称'T' )

错误2无法找到类型或名称空间名称'T'(您是否缺少using指令或程序集引用?)

错误3无法找到类型或名称空间名称'T'(您是否缺少using指令或程序集引用?)


解决方案

  public static class Utility 
{
public static IEnumerable< T> Filter1< T>(//在函数
上输入参数,这个IEnumerable< T>输入,Func< T,bool>谓词)
{

如果你不关心它的扩展方法,你可以为这个类添加一个通用约束。我的猜测是你想要的扩展方法。

  public static class Utility< T> //在类
{
中输入参数public static IEnumerable< T> Filter1(//不再是扩展方法
IEnumerable< T> input,Func< T,bool>谓词)
{


I have following code that I am compiling in a .NET 4.0 project

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;  
                }  
            }  
        }  
    }  
}  

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

解决方案

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

If you dont care if its an extension method or not, you can add a generic constrain to the class. My guess is you want the extension method.

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)  
    {  

这篇关于无法找到类型或名称空间名称“T”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆