0

I have this simple code:

var result = Enumerable.Range(1, 100000000).ToArray();
var range = string.Join("", result);

What I am trying to do is generate all digits on a range of (1 to 10^8) or (1 to 100000000)

So in order to do that, I use Enumerable.Range but I get an exeption on the second line of my code which is joining them after the range was made.

I get:

Exception of type 'System.OutOfMemoryException' was thrown.

I need to get:

1234567891011121314... and so on. (without space)

Any idea why?

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • 1
    See the top 2 answers. Let me know if this is **not** a duplicate and I'll reopen – Jeremy Thompson Aug 25 '17 at 03:43
  • 1
    _"my largest input is 10^8"_ -- largest input for what? What is it you're actually trying to do? On the face of it, the code you posted is just plain ridiculous. In what scenario is it useful to have a string made up of 100 million different numbers concatenated together? What is it that you're _trying_ to do? – Peter Duniho Aug 25 '17 at 03:43
  • _"I tried other solutions on SO but this case is different"_ -- other solutions to what? What did you try? Why is your case different? – Peter Duniho Aug 25 '17 at 03:46
  • I edited my question on what I want to do and the output I am expecting. – Willy David Jr Aug 25 '17 at 03:49
  • 1
    As the answer says, no single object in a .Net program may be over 2GB. Joining every number from `1` to `10^8` exceeds that limit, hence the `OutOfMemoryException`. – Mr Awesome8 Aug 25 '17 at 03:55
  • If the duplicate is not enough to help you, you should probably open a new question and ask about what you actually want to do. Typically, a string in program memory is just dead weight unless it is *used* somewhere (print, write to file, compute something). If you explain your usage, you may get suggestions on what to do *instead of* creating a single string. – grek40 Aug 25 '17 at 06:40

0 Answers0