0

I have some folder that might contain a lot of files, I want to know what's the maximum size that string array can hold, I mean how many file names the array:

string[] files=Directory.GetFiles(@"c:\Dir\"); can hold?

Note that I'm asking about string array, not something else please.

CPU
  • 91
  • 1
  • 7
  • @Fruchtzwerg it talks about **int** array, I'm asking about **string** array, please read carfuly – CPU Sep 16 '18 at 07:26
  • 1
    That makes no difference. – Fruchtzwerg Sep 16 '18 at 07:27
  • Array size limit is big enough that it's unlikely to to be a problem, but if you are still afraid, use `Directory.EnumerateFiles()` instead - it returns IEnumerable. – Ňuf Sep 16 '18 at 08:08

1 Answers1

1

Array Class

By default, the maximum size of an Array is 2 gigabytes (GB). In a 64-bit environment, you can avoid the size restriction by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment. However, the array will still be limited to a total of 4 billion elements, and to a maximum index of 0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and arrays of single-byte structures).

Very useful comment by Ňuf

But is should be noted that strings themself do not count towards the 2GB size limit, because the array contains only references to these strings. So the maximal number of elements in string array is approx. 500M in 32bit process and 2G in 64bit process. Also this limit only applies to .NET CLR, other implementations may have different limits (e.g. Mono on 64bit supports even larger arrays with –enable-big-arrays option)

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • That's also relevante for **string** array? – CPU Sep 16 '18 at 07:28
  • 2
    @CPU yes its relevant for arrays of any type – TheGeneral Sep 16 '18 at 07:29
  • But is should be noted that strings themself do not count towards the 2GB size limit, because the array contains only references to these strings. So the maximal number of elements in string array is approx. 500M in 32bit process and 2G in 64bit process. Also this limit only applies to .NET CLR, other implementations may have different limits (e.g. Mono on 64bit supports even larger arrays with `–enable-big-arrays` option) – Ňuf Sep 16 '18 at 07:46