Console.In.Peek() returns -1 on enter

后端 未结 2 897
野性不改
野性不改 2021-01-25 17:41

I would have expected the following C# program to only print \"EOF!\" once I hit \"Ctrl-Z\" in the console. Instead, the program finishes as soon as I hit Enter:



        
2条回答
  •  梦毁少年i
    2021-01-25 18:04

    Hitting Ctrl-Z will produce the value 26 from Console.In.Peek(); You have to close the input stream in order to produce a -1 (happens when you close the console, hit Ctrl-C (by default), or explicitly call Console.In.Close()).

    Also, by default the console streams will operate in line mode, which means that the stream won't actually be filled with characters until you hit enter. You can use 'Console.ReadKey', which blocks (see http://msdn.microsoft.com/en-us/library/system.consolekeyinfo.key.aspx), or you can switch the console out of line mode. A C# example of this can be found here: http://ewbi.blogs.com/develops/2005/11/net_console_pre.html.

提交回复
热议问题