简体   繁体   English

获取错误找不到类型或命名空间名称“T”(您是否缺少using指令或程序集引用?)

[英]Getting error The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

I am getting compile error error "The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference? 我收到编译错误错误“无法找到类型或命名空间名称'T'(您是否缺少using指令或程序集引用?

Below is my code: 以下是我的代码:

static class ExtensionMethods
{
    public static Collection<T> ToCollection(this IEnumerable<T> source)
    {
        Collection<T> sourceCollection = new Collection<T>();

        foreach (T currentSourceInstance in source)
        {
            sourceCollection.Add(currentSourceInstance);
        }

        return sourceCollection;
    }
} 

Change it to this: 把它改成这个:

public static class ExtensionMethods
{
    public static Collection<T> ToCollection<T>(this IEnumerable<T> source)
    {
        Collection<T> sourceCollection = new Collection<T>();

        foreach (T currentSourceInstance in source)
        {
            sourceCollection.Add(currentSourceInstance);
        }

        return sourceCollection;
    }
} 

Notice the ToCollection<T> , otherwise the compiler doesn't understand where this T is coming from. 注意ToCollection<T> ,否则编译器不知道这个T来自何处。

You can call it like this (where Thing is your custom type in this example): 你可以像这样调用它(在这个例子中Thing是你的自定义类型):

var items = new List<Thing>();
var collection = items.ToCollection<Thing>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到类型或命名空间名称“T”(您是否缺少using指令或程序集引用?) - The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) 为什么会出现这个错误? “找不到类型或名称空间名称&#39;c&#39;(您是否缺少using指令或程序集引用?)” - Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)” 错误1找不到类型或名称空间名称&#39;FPSTimer&#39;(您是否缺少using指令或程序集引用?) - Error 1 The type or namespace name 'FPSTimer' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“BannerPosition”是否缺少 using 指令或程序集引用? - error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? c#错误1找不到类型或名称空间名称”(是否缺少using指令或程序集引用?) - c# Error 1 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) 错误:找不到类型或命名空间名称“SqlCe”(您是否缺少 using 指令或程序集引用?) - Error: The type or namespace name 'SqlCe' could not be found (are you missing a using directive or an assembly reference?) 错误1找不到类型或名称空间名称&#39;ProductServiceClient&#39;(您是否缺少using指令或程序集引用?) - Error 1 The type or namespace name 'ProductServiceClient' could not be found (are you missing a using directive or an assembly reference?) 错误14找不到类型或命名空间名称“Document”(您是否缺少using指令或程序集引用?) - Error 14 The type or namespace name 'Document' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246 找不到类型或命名空间名称“CreateRandomAnswersForKey”(您是否缺少 using 指令或程序集引用?)? - Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM