8

Possible Duplicate:
What is the maximum possible length of a .NET string?

Is there limit for a C# string to hold data?

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426

2 Answers2

11

Strings cannot have more than 231 characters, since String.Length is a 32-bit integer.

They're also limited by available memory.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 5
    In the Microsoft CLR the max length would actually be 2**30 characters, since the CLR imposes a hard 2GB per-object size limit, and each UTF-16 character takes 2 bytes. (Not sure whether other implementations, eg Mono, also impose this restriction.) – LukeH Dec 20 '10 at 01:56
0

string.Length is int, so string can contain Int.MaxInt bytes - 2,147,483,647

  • 1
    This is an example of poor logic, because while it is true that String.Length returns an integer and the max value of an integer is 2,147,483,647, it is incorrect on more than one level to assume that a string can store as many characters as integer can hold. – WonderWorker Oct 08 '19 at 11:24