0

I know in C# you can do int.MaxValue or long.MaxValue to get the maximum for these two types, I was wondering if there are similar ways to get the maximum length for the string.

I don't see it here. Also, I am aware that there are already questions being asked on max string length. Max string length in C#

I am asking if there is an existing function that comes in handy where you can use it to set the condition when and when not to truncate your string to avoid program crashing. Or what is the normal approach?

E.g. Maybe something as easy as Max(string.length)?

csamleong
  • 769
  • 1
  • 11
  • 24
  • If you are aware of that question - it also gives the answer – BugFinder Jun 22 '18 at 09:56
  • No. In the same way that there is no known method to find the max length of an array. Until the array is allocated you can't know if there is enough virtual address space and enough physical memory to contain it. – xanatos Jun 22 '18 at 09:58

1 Answers1

1

No. In the same way that there is no known method to find the max length of an array you can allocate. Until you try to allocate the memory, you can't know if there is enough virtual address space and enough physical memory to contain it. Theorically you could VirtualAlloc (Windows API) greater and greater memory blocks until it fails, deallocate the block and then try to allocate the same amount of memory in .NET, knowing that the memory is there so the .NET should be able to allocate it.

Note that this is true at 32 bits... I haven't ever seen an out-of-memory error at 64 bits.

xanatos
  • 109,618
  • 12
  • 197
  • 280