What's best, return a large string or pass via "ref"?

  • Thread starter Thread starter Bob Maggio
  • Start date Start date
B

Bob Maggio

I have created a function that returns a string containing raw HTML to
be used on a web form (see below):

private string GetReportHTML()
{
string strHTML = "";

// I have some code here that queries a database and
// formats the return DataSet as HTML and inserts
// this HTML into strHTML for return to the calling
// function

return strHTML;
}

This HTML content is a report. As such, there is potential for this
report, and thus the returned string size, to be very large.

My question is this, would there be any performance benefit by
changing this function from returning a string to accepting a "ref
string strHTML", considering this string could be very large? (see
below)

private void GetReportHTML(ref strHTML)
{
}

Thanks in advance.
 
Since the string type is a reference type, a reference to the string is
already returned from the GetReportHTML() method. Adding a parameter with
the ref keyword won't do anything for you. A ref param would only be used
if you wanted to change which object the parameter referred to, which is not
what you want to do in this case.

Joe
 
No. You are already passing back a reference to a string anyway.

-Rob Teixeira [MVP]
 
You probably should to do it a different way altogether. If the report is
large then the string will need to be created in memory. If you have a lot
of people hitting your site you will use much memory.
 
Back
Top