Home All Groups Group Topic Archive Search About

string or StringBuilder return ?

Author
14 Sep 2006 11:14 AM
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

Author
14 Sep 2006 1:58 PM
Phill W.
pamelaflue***@libero.it wrote:

> 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.



Show quoteHide quote
>
> -P
>
Author
14 Sep 2006 3:46 PM
pamelafluente
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:

Show quoteHide quote
> pamelaflue***@libero.it wrote:
>
> > 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.
>
>
>
> >
> > -P
> >
Author
15 Sep 2006 8:45 AM
Brian Cryer
<pamelaflue***@libero.it> wrote in message
news:1158248797.539586.62120@k70g2000cwa.googlegroups.com...
> 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).
--
Brian Cryer
www.cryer.co.uk/brian
Author
15 Sep 2006 3:40 PM
pamelafluente
Thanks. Now I can see it :)

Brian Cryer ha scritto:

Show quoteHide quote
> <pamelaflue***@libero.it> wrote in message
> news:1158248797.539586.62120@k70g2000cwa.googlegroups.com...
> > 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).
> --
> Brian Cryer
> www.cryer.co.uk/brian