C# 创建一个接受用户输入的列表,然后在特定值范围内打印列表中的数字。(请解释它的工作原理和原因)

C# Creating a list that takes user input and after prints the number from the list within a certain value range.(please explain how and why it works)

using System;
using System.Collections.Generic;
namespace exercise_69
{
  class Program
  {
    public static void Main(string[] args)
     {
  
 
   List<int> numbers = new List<int>();
      //creating a list called numbers
    while (true)
      {
     int input = Convert.ToInt32(Console.ReadLine());
     if (input == -1)
        {
      break;
        }
    numbers.Add(input);
      }
    //fills a list called numbers until user enters " -1 "
  
    Console.WriteLine("from where");
    int lownum = Convert.ToInt32(Console.ReadLine());
      //lowest number to get printed
    Console.WriteLine("where to");
    int highnum = Convert.ToInt32(Console.ReadLine());
     //highest number to get printed 

    foreach(int number in numbers)
      {
       if(lownum < number || highnum > number)
       {
         Console.WriteLine(numbers);
       }  //trying to filter the numbers and print them 
          
              

      }          
    }
  }
} 
  

Blockquote

我遇到的问题是当我 运行 控制台告诉我这个程序时 “System.Collections.Generic.List`1[System.Int32]” 所以我的问题是如何正确过滤或删除列表中某个值(不是索引)内的数字

the console just tells me this "System.Collections.Generic.List`1[System.Int32]"

那是因为你这样做了:

Console.WriteLine(numbers);

numbers 是一个 List<int>,它是一整组数字,而不仅仅是一个数字。 Console.WriteLine 有许多变体(“超载”)知道如何做各种不同的事情。它有一个用于数字、字符串等的 large quantity of specific variations,并且有一个变体就像一个“包罗万象”——它接受一个 object,这意味着它可以接受 C# 世界中的几乎任何东西。如果您确实设法最终使用这种变体(过载),它唯一能做的就是对您传入的任何内容调用 ToString(),然后打印它返回的字符串。

因为您传递了一个 List<int>,并且 Console.WriteLine 没有任何变体可以专门对 List 做任何很酷的事情,这意味着您传递的 List 会被 catch-所有版本的 WriteLine; “根据传入的内容调用 ToString 并打印结果”。因为 List 本身没有非常具体或有趣的 ToString(),它只是从 object 继承了 ToString() 的一个版本,这是 C# 中所有事物的最简单的根源。 Object 的 ToString() 并没有做太多 - 它只是 returns 对象的类型,在这种情况下,它是一个“System.Collections.Generic.List`1[System.Int32]”.. 这就是为什么你看到你在控制台中看到的

既然你知道为什么你的代码打印列表的类型,因为你正在传递一个列表,你能看到如何改变它以便你传递其他东西(比如,例如一个实际的您要打印的号码)?

Console.WriteLine(numbers);
                  ^^^^^^^ 
           this needs to be something else - can you work out what?