C# Mono Console:如何修复输出中的第一行

C# Mono Console : how fix first line in out put

我想在控制台中绘制一个 table,所以当我在 table 中有很多信息时(比如超过 20 行),我丢失了 table 的标题(名称日期时间下载)

如何修复前 3 行,

------------------------------------------------
Name    Date    Time    Download
------------------------------------------------
text1   28/10   13:11   not yet
text2   28/10   13:14   not yet
text3   28/10   13:19   not yet
text4   28/10   13:25   not yet
text5   28/10   13:45   not yet
text6   28/10   13:50   not yet
  .       .       .        .
  .       .       .        .
  .       .       .        .
text35  28/10   13:59   not yet

只需检查您的项目是否多于您可以显示的数量,如果是这种情况,请停止打印项目并打印您的空行。

这是一个简单粗暴的例子:

void Main()
{
    var input = Enumerable.Range(1, 35).Select(e => "Line: " + e).ToList();

    Console.WriteLine("------------------------------------------------");
    Console.WriteLine("THE HEADER");
    Console.WriteLine("------------------------------------------------");

    foreach (var element in GetLines(input))
        Console.WriteLine(element);
}

IEnumerable<string> GetLines(IList<string> input, int maxLines = 10)
{
    for (int i = 0; i < input.Count; i++)
    {
        yield return input[i];

        if (i == maxLines - 5 && input.Count > maxLines)
        {
            yield return " . . . .";
            yield return " . . . .";
            yield return " . . . .";
            yield return input[input.Count - 1];
            yield break;
        }
    }
}

它将打印

------------------------------------------------
THE HEADER
------------------------------------------------
Line: 1
Line: 2
Line: 3
Line: 4
Line: 5
Line: 6
 . . . .
 . . . .
 . . . .
Line: 35