多少空间的String.Empty采取CLR [英] How much space does string.Empty take in CLR

查看:177
本文介绍了多少空间的String.Empty采取CLR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多少空间的String.Empty 参加CLR?
我猜它只是一个字节为 NULL 字符。

How much space does string.Empty take in CLR?
I'm guessing it's just one byte for the NULL character.

推荐答案

没有,该字符串是一个完整的对象,与对象头(包含类型引用,同步块等),长度和字符的任何要求.. 。这将是一个空字符(两个字节)和适当的填充,以四舍五入到4个或8个字节的整体。

No, the string is a complete object, with an object header (containing a type reference, sync block etc), length, and whatever characters are required... which will be a single null character (two bytes) and appropriate padding to round up to 4 or 8 bytes overall.

请注意,尽管在.NET中字符串的长度场,他们仍然以空值终止为求互操作的。空字符的没有的包含在长度。

Note that although strings in .NET have a length field, they're still null-terminated for the sake of interop. The null character is not included in the length.

当然,的String.Empty 将只引用一个对象,无论多少次,你使用它......但引用的将有4个或8个字节,所以如果你有:

Of course, string.Empty will only refer to a single object no matter how many times you use it... but the reference will be 4 or 8 bytes, so if you have:

string a = string.Empty;
string b = string.Empty;
string c = string.Empty;

这将是三个引用(12或24字节)全部指的是相同的对象(这可能是大约20或大小24字节)。

that will be three references (12 or 24 bytes) all referring to the same object (which is probably around 20 or 24 bytes in size).

这篇关于多少空间的String.Empty采取CLR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆