English 中文(简体)
C# - What does "" equate to?
原标题:

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

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 ""?

Anyone know what it is?

最佳回答

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.

问题回答

It s a string with a null character. Older string libraries — like that used in C or older C++ libraries — used the 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 , such that it s a common point of error. Testing libraries like Pex will use it to make sure your program handles it correctly.

It s a string containing the character . 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.

Escape Sequence  
    
Character Name 
Null    
Unicode Encoding     
0x0000

See this link.

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

In C, which uses 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 as well as being terminated with it. You could safely modify str[0], or strncat a single character into it.

I just found a good example in which is very important and necessary.

Assume we want to remove the last unwanted , in the following code.

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write($"{a},");
}

enter image description here

If we only add Console.Write(" "); as follows,

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write($"{a},");
    Console.Write("
");
}

The output will be still the same.

But if we add as follows,

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write($"{a},");
    Console.Write("
");
} 

The unwanted trailing , vanishes.

enter image description here





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签