如何在 C# 中使用返回 2d System.Array(不是 string[,])的方法?
我正在使用这样的行:
Array stringArray = Array.CreateInstance(typeof(String), 2, 2);
使用一个返回 2D System.Array 和我的 stringArray 变量的方法。但是 stringArray = MyClassInstance.Work(...,... ...);未填充该函数的返回数组。
我应该如何定义我的集合变量,以便将其分配给返回 2D System.Array 字符串数组值的方法的返回值?
谢谢
I am using a line like this:
Array stringArray = Array.CreateInstance(typeof(String), 2, 2);
to consume a method that returns a 2D System.Array with my stringArray variable. However stringArray = MyClassInstance.Work(...,... ...); is not filled with the return array of that function.
How should I define my collection variable so that it can be assigned to the return value of a method which returns a 2D System.Array string array value?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Array.CreateInstance
返回一个Array
,未指定其维度和长度(但无论如何它是一个System.Array
,它是所有数组的基类在 C# 中)。在大多数情况下,方法的返回数组更像是:Array.CreateInstance
returns anArray
, without its dimension and length specified(but anyway it's anSystem.Array
which is the base class of all arrays in C#). In most circumstances a return array from a method is more like:在 2D 数组上:
这将返回一个
string[,]
转换为Array
。它与(Array)(new string[2,2])
相同。因此,如果需要,您可以将其转换为string[,]
。但你为什么要使用它呢?难道不能只使用
string[,]
来代替吗?难道你不知道元素类型在编译时是string
吗?也许可以使用通用参数。关于您过度服务的问题:
除非您将代码发布到发生问题的位置,否则我们无法帮助您解决该部分问题。看起来 MyClassInstance.Work 没有返回您创建的数组实例。但是除非您显示带有错误的代码,否则我无法告诉您它在哪里。
On your 2D-Array:
This returns a
string[,]
cast toArray
. It's identical to(Array)(new string[2,2])
. So you can cast it tostring[,]
if you want.But why do you want to use that anyways? Can't you just use a
string[,]
instead? Don't you know that the element type isstring
at compile-time? Perhaps make in a generic parameter.On your overserved problem:
We can't help you with that part of the problem unless you post the code where it happens. It seems like the MyClassInstance.Work doesn't return the array instance you created. But unless you show the code with the bug I can't tell you where it is.