'string[]'不包含'Cast'的定义

时间:2022-04-29 16:56:54

I am getting the error:

我得到的错误是:

'string[]' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)

'string[]'不包含'Cast'的定义,也没有找到接受'string[]'类型第一个参数的扩展方法'Cast'(您是否缺少使用指令或程序集引用?)

on the following piece of code:

关于下面这段代码:

return mNames.Cast().ToArray();

What using directive or assembly reference do I need? How do I find out such things?

使用指令或汇编引用我需要什么?我怎么发现这些东西?

I'm a noob at C# and .NET, I'm just copying code to get a job done, so don't get too technical with me.

我是c#和。net的新手,我只是复制代码来完成工作,所以不要对我太过技术性。

5 个解决方案

#1


18  

(1) Make sure you are working on C# 3.0+

(1)确保你正在开发c# 3.0+

(2) Make sure your code contains:

(2)确保您的代码包含:

using System.Linq;

(3) .Cast is a generic method, you need to specify the type parameter, like this:

(3).Cast是一个泛型方法,您需要指定类型参数,如下所示:

return mNames.Cast<AnotherType>().ToArray();

#2


5  

That usually happens when you're missing using System.Linq; at the top of your file.

这通常发生在你使用System.Linq的时候;在文件的顶部。

You'll also need to be using .NET 3.5 or greater for it to work. System.Linq is in the assembly System.Core.dll, which is included by default in projects that use .NET 3.5 or higher.

您还需要使用。net 3.5或更高版本才能使其正常工作。系统。Linq在装配系统中。在使用。net 3.5或更高版本的项目中默认包含的dll。

EDIT

编辑

On closer inspection, that code will never work as written, because the Enumerable.Cast() method is generic, and requires you to pass in the type that you are casting to: e.g. mNames.Cast<object>().ToArray();

在更仔细的检查中,该代码将永远不会像编写的那样工作,因为枚举. cast()方法是通用的,并且要求您传递您要转换到的类型:例如mNames.Cast().ToArray();

#3


2  

Usually, you call the Cast<T>() extension method with a type argument, like mNames.Cast<SomeType>().

通常,使用类型参数调用Cast ()扩展方法,如mNames.Cast ()。

Anyway, mNames already seems to be a string[], so what do you want to cast it to? Casting to object wouldn't be necessary, because object[] can be assigned from string[].

无论如何,mname似乎已经是字符串[]了,那么您想把它转换成什么呢?不需要对对象进行强制转换,因为对象[]可以从字符串[]中分配。

#4


2  

Even if you don't explicitly use "Cast" this compile error will also come up if you use Linq expressions and forget "using System.Linq;"

即使您没有显式地使用“Cast”,如果使用Linq表达式并忘记“使用System.Linq”,也会出现这个编译错误。

#5


-1  

use primary.AddRange(secondary);

使用primary.AddRange(二级);

#1


18  

(1) Make sure you are working on C# 3.0+

(1)确保你正在开发c# 3.0+

(2) Make sure your code contains:

(2)确保您的代码包含:

using System.Linq;

(3) .Cast is a generic method, you need to specify the type parameter, like this:

(3).Cast是一个泛型方法,您需要指定类型参数,如下所示:

return mNames.Cast<AnotherType>().ToArray();

#2


5  

That usually happens when you're missing using System.Linq; at the top of your file.

这通常发生在你使用System.Linq的时候;在文件的顶部。

You'll also need to be using .NET 3.5 or greater for it to work. System.Linq is in the assembly System.Core.dll, which is included by default in projects that use .NET 3.5 or higher.

您还需要使用。net 3.5或更高版本才能使其正常工作。系统。Linq在装配系统中。在使用。net 3.5或更高版本的项目中默认包含的dll。

EDIT

编辑

On closer inspection, that code will never work as written, because the Enumerable.Cast() method is generic, and requires you to pass in the type that you are casting to: e.g. mNames.Cast<object>().ToArray();

在更仔细的检查中,该代码将永远不会像编写的那样工作,因为枚举. cast()方法是通用的,并且要求您传递您要转换到的类型:例如mNames.Cast().ToArray();

#3


2  

Usually, you call the Cast<T>() extension method with a type argument, like mNames.Cast<SomeType>().

通常,使用类型参数调用Cast ()扩展方法,如mNames.Cast ()。

Anyway, mNames already seems to be a string[], so what do you want to cast it to? Casting to object wouldn't be necessary, because object[] can be assigned from string[].

无论如何,mname似乎已经是字符串[]了,那么您想把它转换成什么呢?不需要对对象进行强制转换,因为对象[]可以从字符串[]中分配。

#4


2  

Even if you don't explicitly use "Cast" this compile error will also come up if you use Linq expressions and forget "using System.Linq;"

即使您没有显式地使用“Cast”,如果使用Linq表达式并忘记“使用System.Linq”,也会出现这个编译错误。

#5


-1  

use primary.AddRange(secondary);

使用primary.AddRange(二级);