C# - What does “\0” equate to?

吃可爱长大的小学妹 提交于 2019-11-26 16:58:33

问题


I am playing with Pex and one of the parameters it passes into my method is "\0".

What does that mean? My guess is an empty string ("") based on the content of my method. However, if it is the same then why not just use "" instead of "\0"?

Anyone know what it is?


回答1:


'\0' is a "null character". It's used to terminate strings in C and some portions of C++. Pex is doing a test to see how your code handles the null character, likely looking for the Poison Null Byte security exploit.

Most C# code has nothing to fear; if you pass your string to unmanaged code, however, you may have problems.

Edit:

Just to be explicit... Pex is passing a string containing a null character. This is not a null reference.




回答2:


It's a string containing the character '\0'. C# doesn't treat this in any particularly special way - it's just unicode character U+0000. If you write:

int firstCodePoint = text[0];

then you'll find firstCodePoint is 0.




回答3:


It's a string with a null character. Older string libraries — like that used in C or older C++ libraries — used the '\0' character to indicate the end of the string.

Newer environments like .Net use a different system, but there is a lot of history around ending a string with '\0', such that it's a common point of error. Testing libraries like Pex will use it to make sure your program handles it correctly.




回答4:


A string of length 1, containing the character \u0000 (aka NUL). This character is not treated specially.

In C, which uses \0 to terminate string, you also allocate a string of length 1. In this case the standard string functions will report a length of 0, since the string contains \0 as well as being terminated with it. You could safely modify str[0], or strncat a single character into it.




回答5:


Escape Sequence  
\0    
Character Name 
Null    
Unicode Encoding     
0x0000

See this link.



来源:https://stackoverflow.com/questions/2292850/c-sharp-what-does-0-equate-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!