Search code examples
.netunit-testingassert

Why would you use Assert.ReplaceNullChars(string input)?


Why would you use the following?

Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ReplaceNullChars(string input)

I have searched but can't find any cases of anyone really using this. Even MSDN is not helpful. All I can find out about it is:

ReplaceNullChars – Replaces Null characters in a string \0 with \\0

This doesn't sound all that useful to me and I'm suprised it's there in the Assert class.


Solution

  • It’s probably so that you can print a string containing null chars for better diagnostics, since printing a string which contains a null char usually doesn’t result in anything meaningful (it will usually be truncated in the output).

    However, a better method would have been Assert.EscapeNonprintableChars – i.e. replace not only \0 but all non-printable characters.