检查Console.ReadLine()!= null [英] Checking Console.ReadLine()!=null

查看:171
本文介绍了检查Console.ReadLine()!= null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序制作了CMD,当我检查 Console.ReadLine!= null

i am making a CMD for my applcation, and figure i have a trouble when i check Console.ReadLine!=null

--Full Code ---

---Full Code---

   string input = Console.ReadLine();
    if(input!=null)
    {
    SomeFunction(input);
    }

---代码结束---

---End Of Code---

拆分此字符串,例如:

  Console.WriteLine(input[0]);

所以问题是:

如果用户点击进入一次。但是如果使用了,我得到一个异常:
[0]不存在。

It works if user hits enter once. But if use does that again i am getting an exception: That [0] does not exist.

推荐答案

您按 ENTER Console.ReadLine 返回空 string 。它不返回 null 。使用 string.IsNullOrEmpty 来检查。

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

if(!string.IsNullOrEmpty(input))

根据文档它将返回null只有当你按 CTRL + Z 。

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

这篇关于检查Console.ReadLine()!= null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆