请教一下,为什么不能正常打印int[] iNums = { 8, 6, 4, 2, 1, 5 };里的内容?谢谢!!
using System;
namespace PrintToScreen
{
class Program
{
static void Main(string[] args)
{
string[] strNums = { "ff", "cc", "bb", "aa", "ee" };
PintToScreen(strNums);//可以正常打印
PintToScreen(6, 4, 2, 8, 3);//可以正常打印
int[] iNums = { 8, 6, 4, 2, 1, 5 };
PintToScreen(iNums);//打印異常System.Int32[],?????????
}
public static void PintToScreen(params object[] obj)
{
Array.Sort(obj);
for(int i =0;iWriteLine(obj[i]);
}
}
}
aa
bb
cc
ee
ff
2
3
4
6
8
System.Int32[]
因为你的形参是params ,所以当你传入一个数组时,不会用实参直接替换掉形参,而是将iNums作为数组的一项传入的,obj是个二维数组
已经搞定了,需要将object基类,在进行强转会int[]:
public static void PintToScreen(params object[] obj)
{
//Array.Sort(obj);
for (int i = 0; i < ((int[])obj[0]).Length; i++)
{
Console.WriteLine(((int[])obj[0])[i]);
}
}