Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringReader or Memory Stream which is resource friendly?

Tags:

c#

vb.net

I have a module which will be responsible for parsing CSV data received from different user via a website interface, and I have to parse that CSV. I was considering to use, TextFieldParser for it.

But before I could implement I was considering what shall be a better approach...

  1. Generating MemoryStream from data received,
  2. or initialising a StringReader from the same input string.

Which one is better & why?

like image 877
Rajeev Avatar asked Apr 13 '26 15:04

Rajeev


1 Answers

Option 1 won't give you a string at all, so if you want to work with a byte array and buffers, go that way but it seems unlikely. If you're doing string processing would strongly recommend Option 2, because with that you can read a line at a time.

As far as I can see the only reason to use a MemoryStream would be if you need to do something more complex that StringReader doesn't handle as you want (otherwise you're reinventing the wheel): encodings, strange line formats, etc.

Having worked with very large files (specifically CSV files) with StringReaders, I've never had a problem. I'd wager that when MS designed StringReader to do exactly what you're trying to do, they made it as resource-friendly as possible.

like image 176
Kieren Johnstone Avatar answered Apr 16 '26 03:04

Kieren Johnstone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!