如果你这样写程序:
- 你不需要打电话
System.Environment.Exit(0);
- 你也不需要检查输入键。
例子:
class Program
{
static void Main(string[] args)
{
//....
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
}
另一个例子:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press Enter in an emplty line to exit...");
var line= "";
line = Console.ReadLine();
while (!string.IsNullOrEmpty(line))
{
Console.WriteLine(string.Format("You entered: {0}, Enter next or press enter to exit...", line));
line = Console.ReadLine();
}
}
}
又一个例子:
如果需要,可以检查读取的值Console.ReadLine()
是否为空,然后Environment.Exit(0);
//...
var line= Console.ReadLine();
if(string.IsNullOrEmpty(line))
Environment.Exit(0)
else
Console.WriteLine(line);
//...