问题描述
我是 C# 新手,正在研究数组.
I'm new to C# and looking at arrays.
给定:
int[][] myJagArray = new int[5][];
为什么下面打印 j (System.Int32[]) 的类型,而不是每个 j 的内容?
Why does the following print the types of j (System.Int32[]), and not each j's contents?
foreach (int[] j in myJagArray)
{
Console.WriteLine("j : {0}",j);
}
推荐答案
因为Array.ToString()
不返回数组内容,而是返回类型名,而Console.WriteLine
对作为参数发送的每个对象隐式调用 ToString()
.
Because Array.ToString()
does not return the contents of the array, it returns the type name, and Console.WriteLine
implicitly calls ToString()
on each object you send it as a parameter.
这与数组是多维数组的一部分这一事实无关,这只是 CLR 开发人员选择(或者更确切地说,选择不)实现 System.Array 上的 >ToString().
This has no regard to the fact that the array is part of a multi-dimensional array, it is simply the way the CLR developers chose to (or rather, chose not to) implement ToString()
on System.Array
.
这篇关于C#多维数组迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!