什么是.NET字符串的内部格式?字符串、格式、NET

2023-09-03 16:36:49 作者:渣男!

我在做一些pretty的字符串处理密集型code在C#.NET和有好奇一些乔尔Spolsky的文章中,我记得读了一段时间后:

I'm making some pretty string-manipulation-intensive code in C#.NET and got curious about some Joel Spolsky articles I remembered reading a while back:

http://www.joelonsoftware.com/articles/fog0000000319.html < BR> http://www.joelonsoftware.com/articles/Uni$c$c.html

那么,如何.NET办呢?每个字符两个字节?也有一些统一code字符^需要超过使得H ^ H ^ H ^ H ^ H code点。又是怎样连接codeD的长度是多少?

So, how does .NET do it? Two bytes per char? There ARE some Unicode chars^H^H^H^H^H code points that need more than that. And how is the length encoded?

推荐答案

在乔恩斯基特变成了这里是一个链接到他的优秀的琴弦博客在C#。

Before Jon Skeet turns up here is a link to his excellent blog on strings in C#.

在当前的实现中,至少,琴弦占用20+(N / 2)* 4个字节(四舍五入的n / 2下的值),其中n是字符串中的字符数。 string类型是不寻常的,对象本身的大小而定。

In the current implementation at least, strings take up 20+(n/2)*4 bytes (rounding the value of n/2 down), where n is the number of characters in the string. The string type is unusual in that the size of the object itself varies