我目前正在处理大量数组,为了调试目的,我编写了一个通用的Print()方法来打印不同类型的数组
static void Main() { Print(new double[]{ 1, 2, 3 }); // Output: [ 1, 2, 3 ] } static void Print<T>(T[] array) { int size = array.Length; if (size == 0) return; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); }到目前为止工作正常。 然后我想打印一个数组数组,如double[][] ,并尝试以下方法:
static void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print<T>(T[] array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print<T>(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }我只想检查array的元素是否是数组,如果是,我再次为array每个element调用函数Print。 但Print(element)不起作用,因为element是T类型而不是T[] ,我不知道如何告诉编译器在这种情况下T是一个数组。 我需要做些什么来完成这项工作?
I am currently working with lots of arrays and for debugging purposes I wrote a generic Print() method to print different kinds of arrays
static void Main() { Print(new double[]{ 1, 2, 3 }); // Output: [ 1, 2, 3 ] } static void Print<T>(T[] array) { int size = array.Length; if (size == 0) return; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); }which works fine so far. Then I wanted to print an array of arrays, like double[][], and tried the following:
static void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print<T>(T[] array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print<T>(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }I just wanted to check if the elements of array again are arrays, and if so, I call the function Print again for each element of array. But Print(element) doesn't work, since element is of type T and not T[] and I don't know how to tell the compiler that in this case T is an array. What do I have to do to make this work?
最满意答案
你可以用动力学做到这一点:
void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print(dynamic array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }如果您只想测试较小部分的代码 - 我建议使用LinqPad,你有AnyType.Dump()方法,你不能实现任何东西;)
You can do it using dynamics:
void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print(dynamic array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }If you only want to test smaller part of codes - I suggest using LinqPad where you have AnyType.Dump() method and you mustn't implement anything ;)
更多推荐
发布评论