StringBuilder vs. String

0 votes
I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference between the two?

In what case is String builder a better choice?
Sep 21, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
56 views

1 answer to this question.

0 votes

To clarify what Gillian said about 4 string, if you have something like this:

string a,b,c,d;
 a = b + c + d;

then it would be faster using strings and the plus operator. This is because (like Java, as Eric points out), it internally uses StringBuilder automatically (Actually, it uses a primitive that StringBuilder also uses)

However, if what you are doing is closer to:

string a,b,c,d;
 a = a + b;
 a = a + c;
 a = a + d;

Then you need to explicitly use a StringBuilder. .Net doesn't automatically create a StringBuilder here, because it would be pointless. At the end of each line, "a" has to be an (immutable) string, so it would have to create and dispose a StringBuilder on each line. For speed, you'd need to use the same StringBuilder until you're done building:

string a,b,c,d;
StringBuilder e = new StringBuilder();
 e.Append(b);
 e.Append(c);
 e.Append(d);
 a = e.ToString();
answered Sep 21, 2018 by Upasana
• 8,530 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Intel Galileo Vs. Intel Edison?

Intel Galileo Key Features: Type: Single-Board Computer CPU: Intel Quark ...READ MORE

answered Aug 20, 2018 in IoT (Internet of Things) by DataKing99
• 8,130 points
465 views
0 votes
1 answer

What is the difference between IoTivity vs AllJoyn ?

Architecture: IoTivity provides 4 basic components: Discovery Data transmission Data Management Device ...READ MORE

answered Sep 6, 2018 in IoT (Internet of Things) by Annie97
163 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Is there a .NET equivalent to Apache Hadoop?

Microsoft dropped its alternative (Dryad) in favor of Hadoop. ...READ MORE

answered Sep 18, 2018 in Big Data Hadoop by Frankie
• 9,810 points
63 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
30 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
40 views
0 votes
1 answer