Are C# Strings (and other .NET API's) limited

2019-01-26 10:03发布

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'.

8条回答
啃猪蹄的小仙女
2楼-- · 2019-01-26 10:30

Seems like a fundamental problem with the .NET API...

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?

  1. An additional 32 bits of memory would be required anywhere you wanted to know the length of a string.
  2. 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).

查看更多
祖国的老花朵
3楼-- · 2019-01-26 10:33

If you are working with a file that is 2GB, that means you're likely going to be using a lot of RAM, and you're seeing very slow performance.

Instead, for very large files, consider using a MemoryMappedFile (see: http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx). Using this method, you can work with a file of nearly unlimited size, without having to load the whole thing in memory.

查看更多
登录 后发表回答