Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#int和数组的交错数组_C# - Fatal编程技术网

C#int和数组的交错数组

C#int和数组的交错数组,c#,C#,我找不到如何执行以下数组: { 3, 5, 15, { 4, 75, { 25 } } } 它必须是Int和Array的混合体。 我的函数代码如下: p.myMethod(new int[]{ 3, 5, 15, new int[] { 4, 75, new int []{ 25 } } }) 但它不起作用。 我怎样才能得到预期的结果 它必须是Int和Array的混合体 让数组包含多种类型的对象的唯一方法是使用所有元素的基类类型声明和初始化数组,因此在这种情况下,您必须使用对象: objec

我找不到如何执行以下数组:

{ 3, 5, 15, { 4, 75, { 25 } } }
它必须是Int和Array的混合体。 我的函数代码如下:

p.myMethod(new int[]{ 3, 5, 15, new int[] { 4, 75, new int []{ 25 } } })
但它不起作用。 我怎样才能得到预期的结果

它必须是Int和Array的混合体

让数组包含多种类型的对象的唯一方法是使用所有元素的基类类型声明和初始化数组,因此在这种情况下,您必须使用
对象

object[] array = new object[]{ 3, 5, 15, new object[] { 4, 75, new object []{ 25 } } };
这显然是一个问题,在以后访问它们时,您需要知道每个数组项的类型,因为它们都声明为
对象

请看以下内容:

{ 3, 5, 15, { 4, 75, { 25 } } }
索引0、1、2处的项是整数。索引4中的项目是另一种类型,如下所示:

{ 4, 75, { 25 } }
数组中不能有多个类型。创建数组时,必须指定它将包含的类型

因此,您可以保留上述所有内容的唯一数组类型将是
object
类型的数组,因为.NET中的所有内容都源自
object

像这样:

var a = new object[] { 3, 5, 15, new object[] { 4, 75, new[] { 25 } } };
或者使用
ArrayList

var a = new ArrayList { 3, 5, 15, new ArrayList() { 4, 75, new ArrayList() { 25 } } };

为什么“它不起作用”?您得到的输出是什么?如果您向我们展示了
myMethod
,并解释了为什么您认为“它必须是Int和Array的混合体”,这将非常有帮助。您展示的不是交错数组。您有一些嵌套数组。你确定你想要的是什么吗?函数必须传递任何Int或嵌套数组。例如,myMetod(15);或者我的方法({15,2,{3,2,{5}})。若值仅为5,则求出它们的和。代码是excute,但myMethod无法传递嵌套数组。