473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

string or StringBuilder return ?

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

Sep 14 '06 #1
4 1151
pa***********@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
Sep 14 '06 #2
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:
pa***********@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
Sep 14 '06 #3
<pa***********@libero.itwrote in message
news:11*********************@k70g2000cwa.googlegro ups.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
Sep 15 '06 #4
Thanks. Now I can see it :)

Brian Cryer ha scritto:
<pa***********@libero.itwrote in message
news:11*********************@k70g2000cwa.googlegro ups.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
Sep 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
10
by: Crirus | last post by:
Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH Thanks Crirus
18
by: Paul Roberts | last post by:
Hiya I have a string with about 300+ characters, how can i insert a line break or another character every 50 characters for the whole string? Paul Roberts
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
33
by: sklett | last post by:
I need to take a string, make the first character lowercase and prepend an underscore character to it. Something like this: "California" "_california" Here is the (ugly) solution I cam up...
3
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned...
8
by: simonZ | last post by:
I have Function with string parameteres: public string newLine(String string1,String string2,String string3){ StringBuilder webLine = new StringBuilder(); webLine.Append("<tD nowrap>" +...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.