Checking Console.ReadLine()!=null

前端 未结 4 713
执念已碎
执念已碎 2021-01-06 22:51

I am making a CMD for my application, and figure I have a trouble when I check `Console.ReadLine != null``

string input = Console.ReadLine();
 if(input != nu         


        
4条回答
  •  暖寄归人
    2021-01-06 23:52

    When you hit ENTER, Console.ReadLine returns empty string. It doesn't return null. Use string.IsNullOrEmpty to check instead.

    if(!string.IsNullOrEmpty(input))
    

    According to documentation it will return null only if you press CTRL + Z.

提交回复
热议问题