无法将整个列表打印到控制台 [英] Unable to print whole list to console
问题描述
在控制台上打印列表时,请始终收到以下消息.
Keep getting the following message when printing a list on console.
System.Collections.Generic.List`1[System.Int32]
这是控制台代码.它旨在生成给定长度的斐波那契序列.我尝试使用ToString()方法,但这也不起作用.我已经用Java构建了算法,所以我知道这个问题从根本上讲是C#问题.如果我单独打印列表元素,但无法打印整个列表,则此问题已解决.
This is the console code. It is designed to generate a Fibonacci sequence of a given length. I have tried using the ToString() method, but that doesn't work either. I have built the algorithm in Java so i know that the issue, is fundamentally a C# problem. The issue is resolved if print i print the list elements individually, but i can't print the whole list.
class Program
{
public static void Main(string[] args)
{
Fibonacci fibo = new Fibonacci();
Console.WriteLine(fibo.getSequence(9));
Console.ReadLine();
}
}
class Fibonacci
{
public List<int> getSequence(int length)
{
List<int> results = new List<int>();
results.Add(1);
results.Add(1);
int counter = 0;
while (counter != length - 2)
{
int num1 = results[results.Count - 1];
int num2 = results[results.Count - 2];
results.Add(num1 + num2);
counter++;
}
return results;
}
}
推荐答案
您正尝试直接将对象打印到控制台,尝试遍历列表并在返回的任何位置打印它们.
You are trying to print the object directly to console try iterating over list and print them wherever returned.
for (var item in returned)
Console.WriteLine(item)
(如果使用自定义类型).请记住,您已经定义了它的to字符串方法.
if using a custom Type. keep in mind that you have defined its to string method.
这篇关于无法将整个列表打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!