Console.WriteLine correctly processes empty string

Just find what Console.WriteLinecan handle the string correctly null for example Console.WriteLine((string)null);

Can we assume that most class libraries correctly handle a null reference?

+5
source share
4 answers

Well yes.

You can expect that (almost) all classes will correctly handle null references.

However, I feel that what you mean by “right” does not match what the author of the class means.

For example, if you try to open a file and pass nullfor the path to the opened file, it will not fail, it throws an exception.

, " ", . , , .

, , .

+4

.

, . . MSDN , null.

null API , . . , .

, , null, , , .

+10

, "" . " , ", .

0

, ( , - ) -, , null.

:

  • .. "null", → .
  • Parse → "null", → . (, TryParse 2.0).

On the other hand, when an argument is not required, the method usually "swallows the frog" when it is null and treats it as some default value, such as an empty string. In the case of the WriteLine method of the Console object, it really doesn’t need a line, it just writes it to the console, so it doesn’t matter if it is null.

So, as others say, it depends on which method you are trying to use, and it is better to read before trying to pass null values.

0
source

All Articles