string or StringBuilder return ?

  • Thread starter Thread starter pamelafluente
  • Start date Start date
P

pamelafluente

A curiosity.

When one can choose the Return (from a function) either StringBuilder
(that will be eventually converted to string) or a String, which one
would be better to return, in terms of efficiency and good practices.
Or it's just the same thing ?

Dim sb As New System.Text.StringBuilder

Function Whatever1() As String
....
Return sb.ToString

End Function

Function Whatever2() As System.Text.StringBuilder
....
Return sb

End Function



( I guess that for web services string is better (?) )

-P
 
When one can choose the Return (from a function) either StringBuilder
(that will be eventually converted to string) or a String, which one
would be better to return, in terms of efficiency and good practices.

Depends on what you expect the /caller/ of the function to do with it.
If they are going to continue bolting more bits of strings together,
then send them the the tool to do so, i.e. the StringBuilder.

If all they want is the "end result" of your string building, then just
send back the "finished" string.
( I guess that for web services string is better (?) )

Definitely!
The whole point of web services is that they can be called from just
about /anywhere/. It could be another .Net thing or some Unix
application, or some wierd Soap-compliant program that somebody turned
out on their dusty, old Commodore - only one of those stands any chance
of "understanding" the .Net StringBuilder class. ;-)

HTH,
Phill W.
 
Thanks Phill ;)

Actually, what I was wondering is that if putting a long string in the
return stack is more cumbersome than putting there a stringbuilder,
which is reference type ... or perhaps they have a mechanism so that
strings are anyway passed as a kind of reference ??

-P


Phill W. ha scritto:
 
Thanks Phill ;)

Actually, what I was wondering is that if putting a long string in the
return stack is more cumbersome than putting there a stringbuilder,
which is reference type ... or perhaps they have a mechanism so that
strings are anyway passed as a kind of reference ??

Strings are objects, so whether you use stringbuilder or a string you are
just passing around a reference. So, performance wise, it doesn't matter
which you pass around (although there may be benefits one way or the other
depending on what you then do with it).
 
Back
Top