I have a situation where I need to return the MAX of 2MB sized string from a method. I have seen some web services returning big XML contents as strings. What is the optimum size with which we should transact in value parameter passing and returning?
-
1`Is it OK to return a string with the size around 2 MB from a C# method?` Yes. – sa_ddam213 Aug 27 '13 at 04:58
-
1Instead of returning the varible,try using a reference. – Naren Aug 27 '13 at 05:00
-
Thanks :) I am reading http://stackoverflow.com/questions/10792603/how-are-strings-passed-in-net which might be helpful for any others having same doubt. – Srikanth P Vasist Aug 27 '13 at 05:00
-
What is the maximum size of a string ? http://stackoverflow.com/questions/140468/what-is-the-maximum-possible-length-of-a-net-string – Prabhu Murthy Aug 27 '13 at 05:01
3 Answers
There is no technically correct answer to this because it's perfectly normal to pass any amount of characters as long as it's needed in your program. You need to evaluate if passing such a large string is valueable to your program. If it is, great. If it's not, find a way to shorten it or pass a file path or database identifier instead so the service can get the text from there itself.

- 75,013
- 26
- 93
- 142
-
1Plus, it's important to note that strings are interned, so a 2 MB string won't be an issue. Unless, of course, you built that string by concatenating several million characters one at a time. – Daniel Mann Aug 27 '13 at 05:04
Is it OK to return a string with the size around 2 MB from a C#
if it is local (intranet,network) then it is mostly ok (unless the network is v v old). But, if you are passing it over the internet then you should consider the bandwidth of the client on which the data will be sent/received. Will it slow down your page or not etc. In most cases though it will be fine.
What is the optimum size with which we should transact in value parameter passing and returning
Any size that will not cause your application to slow down is fine.

- 31,833
- 6
- 56
- 65
It is completely okay, the only thing you should keep in mind is:
If a user is using 56kbps connection 2024/56=36.14 sec
if this is not important for your case then no need to worry.

- 28,160
- 11
- 74
- 110