问题描述
今天我注意到,C#的String类返回一个字符串作为一个Int的长度。因为INT永远是32位的,不管是什么结构,这是否意味着一个字符串,只能是2GB的长度不超过?
有一个2GB的字符串将是非常不寻常的,并与它一起present很多问题。然而,大多数.NET API的似乎用'诠释'来传达价值观,如长度和数量。这是否意味着我们永远限制在这适合32位集合大小?
好像与.NET API的一个根本问题。我本来期望像数和长度来控制通过的为size_t相当于被退回。
我不知道我是否会去那么远。
考虑几乎所有的集合类的.NET。机会是它有一个计数
属性,它返回一个 INT
。因此,这表明,类为界,在 int.MaxValue
(2147483647)的大小。这不是一个真正的的问题的;这是一个的限制的 - 和一个完全合理的,在场景中的绝大多数
总之,什么替代呢?有 UINT
- 但那是不符合CLS。此外,还有长
...
如果长度
返回长
?
- 在另外一个32位的内存将需要的任何地方,你想知道一个字符串的长度。
- 在这样做的好处是:我们可以有字符串占用了数十亿的RAM千兆字节。万岁。
试着想象一些code像这样令人难以置信的成本:
//主知道多少个字符
字符串尤利西斯= GetUlyssesText();
//分配大致相当大小的一个全新的字符串
字符串schmulysses = ulysses.Replace(尤利西斯,Schmulysses);
基本上,如果你想字符串
的意思是存储无限量的文本数据结构,你有不切实际的期望。当谈到这个尺寸的物体,它是否有任何需要都保持在内存中(而不是硬盘)变得可疑。
Today I noticed that C#'s String class returns the length of a string as an Int. Since an Int is always 32-bits, no matter what the architecture, does this mean that a string can only be 2GB or less in length?
A 2GB string would be very unusual, and present many problems along with it. However, most .NET api's seem to use 'int' to convey values such as length and count. Does this mean we are forever limited to collection sizes which fit in 32-bits?
Seems like a fundamental problem with the .NET API's. I would have expected things like count and length to be returned via the equivalent of 'size_t'.
I don't know if I'd go that far.
Consider almost any collection class in .NET. Chances are it has a Count
property that returns an int
. So this suggests the class is bounded at a size of int.MaxValue
(2147483647). That's not really a problem; it's a limitation -- and a perfectly reasonable one, in the vast majority of scenarios.
Anyway, what would the alternative be? There's uint
-- but that's not CLS-compliant. Then there's long
...
What if Length
returned a long
?
- An additional 32 bits of memory would be required anywhere you wanted to know the length of a string.
- The benefit would be: we could have strings taking up billions of gigabytes of RAM. Hooray.
Try to imagine the mind-boggling cost of some code like this:
// Lord knows how many characters
string ulysses = GetUlyssesText();
// allocate an entirely new string of roughly equivalent size
string schmulysses = ulysses.Replace("Ulysses", "Schmulysses");
Basically, if you're thinking of string
as a data structure meant to store an unlimited quantity of text, you've got unrealistic expectations. When it comes to objects of this size, it becomes questionable whether you have any need to hold them in memory at all (as opposed to hard disk).
这篇关于是C#字符串(和其它.NET API的)限制为2GB的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!