简体   繁体   English

Console.WriteLine正确处理空字符串

[英]Console.WriteLine correctly handle null string

Just find that Console.WriteLine can handle null string correctly like Console.WriteLine((string)null) ; 只需发现Console.WriteLine可以正确处理null字符串,就像Console.WriteLine((string)null)

Can I assume that most class library handle null reference correctly? 我可以假定大多数类库都能正确处理空引用吗?

No. 没有。

This fully depends on the method you call. 这完全取决于您调用的方法。 See the MSDN documentation per method on what to expect when you pass a null . 有关传递null时的期望,请参见每种方法的MSDN文档。

It's never safe to make assumptions about null when calling into someone else's API or library, especially if you don't have its source code to use as a reference. 调用别人的API或库时,对null进行假设永远是不安全的,特别是如果您没有将其源代码用作参考的话。 Always read the docs. 务必阅读文档。 And if the docs don't say, code defensively. 而且,如果文档未说明,请进行防御性编码。

However, the library methods are programmed in such a manner that when they do not accept a null , they will throw an exception telling you that it's not a valid argument. 但是,对库方法进行编程的方式是,当它们不接受null ,它们将引发异常,告诉您这不是有效的参数。

Well... Yes. 嗯,是。

You can expect that (almost) all classes will handle null references correctly. 您可以期望(几乎)所有类都能正确处理空引用。

However, I feel that what you mean by "correctly" is not the same as what the author of the class means. 但是,我认为您“正确”的含义与课程作者的含义不同。

For instance, if you try to open a file and pass null for the path to the file to open, it will not fail silently, it will throw an exception. 例如,如果您尝试打开文件并为打开的文件路径传递null ,则它不会无提示地失败,它将引发异常。

If that is what you mean by "handle correctly", then sure. 如果那是“正确处理”的意思,那么可以肯定。 In fact, I hope all classes handles null references correctly. 实际上,我希望所有类都能正确处理空引用。

But don't expect the program to just trundle on as if nothing happened. 但是不要指望该程序会像什么都没发生一样陷入困境。

That depends entirely on what you mean by "correctly" and on the method itself. 这完全取决于您“正确”的含义以及方法本身。 Unless you mean "could throw an exception because it was expecting an actual string", then no. 除非您的意思是“可能因为期望实际的字符串而抛出异常”,否则不会。

In general, when the argument (doesn't matter if string or any other type) is required to do something the method will usually crash with exception when that argument is null. 通常,当需要参数(与字符串或任何其他类型无关)执行某项操作时,该参数通常为null时,该方法通常会因异常而崩溃。

Couple of examples: 几个例子:

  1. Files.. can't open "null" file, so --> error. Files ..无法打开“空”文件,所以->错误。
  2. Parse --> can't parse "null", so --> error. 解析->无法解析“ null”,所以->错误。 (Yeah, they added TryParse in 2.0) (是的,他们在2.0中添加了TryParse)

On the other hand, when the argument is not required the method will usually "swallow the frog" when it's null and treat it as some default value, for example empty string. 另一方面,当不需要参数时,该方法通常在为null时会“吞下青蛙”,并将其视为某个默认值,例如空字符串。 In the case of WriteLine method of the Console object, it doesn't really need the string, it simply write it to the console so it doesn't care if it's null. 对于Console对象的WriteLine方法,它实际上并不需要该字符串,它只是将其写入控制台,因此它并不关心它是否为null。

So like others here said, it depends on what method you try to use and you better read before trying to pass null values. 因此,就像这里的其他人所说,这取决于您尝试使用哪种方法,并且在尝试传递空值之前最好阅读一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM