Impact of Gen-AI on IT Jobs - Growth Mindset Show
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
20
Reply
When to use StringBuilders?
Arunava Bhattacharjee
11y
4.4k
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
Generally inside loop block where string value is change frequently we use stringBuilder class object. Actually String object is immutable. Every time we use one of the methods in the System.String class, we create a new string object in memory, which requires a new allocation of space for that new object. so when frequent change in string value is happening it's recommended to use StringBuilders.
Manish Kumar Choudhary
11y
2
when we are concatenating string value for the multiple iterations because the string builder handle large number of character with the greater performance
Mohsin Mukri
8y
0
when we are concatenating string value for the multiple iterations because the string builder handle number of character with the greater performance
Mohsin Mukri
8y
0
When we required string manipulation. StringBuilder is faster than String. using System.Text; namespace in C#
Ashish Srivastava
9y
0
when you want to add or concanate, substract etc. on string manipulation that time you can use stringbuilder. StringBuilder is faster than string.
sushil kumar
9y
0
Definitely use StringBuilder when you're concatenating in a non-trivial loop - especially if you don't know for sure (at compile time) how many iterations you'll make through the loop. For example, reading a file a character at a time, building up a string as you go using the += operator is potentially performance suicide. Definitely use the concatenation operator when you can (readably) specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly - or String.Join if you need a delimiter.) Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance. If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you'll only benefit from using StringBuilder if you don't need the (first name + last name) string for other purpose (as we do in the example which creates a Person object). If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go. Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in. If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways.
Ajay Gandhi
10y
0
Definitely use StringBuilder when you're concatenating in a non-trivial loop - especially if you don't know for sure (at compile time) how many iterations you'll make through the loop. For example, reading a file a character at a time, building up a string as you go using the += operator is potentially performance suicide. Definitely use the concatenation operator when you can (readably) specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly - or String.Join if you need a delimiter.) Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance. If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you'll only benefit from using StringBuilder if you don't need the (first name + last name) string for other purpose (as we do in the example which creates a Person object). If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go. Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in. If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways.
Ajay Gandhi
10y
0
If concatination is going to be done for multiple times to a string variable, then we can go for StringBuilder.
Srikanth Reddy
10y
0
if you want to append large string data then use string builder. for more details refer below linkhttp://www.aspdotnet-suresh.com/2013/11/difference-between-string-and-stringbuilder-csharp.html
Abrar Ahmad Ansari
10y
0
Simple. when you try to build a string...
Md. Raskinur Rashid
10y
0
string builder gives high performance. it mainly used whenever you add the values in runtime(if you don't know how many values are there). it is mutable and it uses the Append or AppendLine methods for Adding new item.
Yadagiri Reddy
11y
0
FOR FASTER PERFORMANCE WHILE USING STRING CONCATENATION ETC USE STRING BUILDERS. STRING BUILDERS ARE MUTABLE.
trilochan
11y
0
When we concatenate string ,or use in strings operations inside loop, use string builder.
Roymon TP
11y
0
When we concatenate string ,or use in strings operations inside loop, use string builder.
Roymon TP
11y
0
When we concatenate string ,or use in strings operations inside loop, use string builder.
Roymon TP
11y
0
http://www.dotnetperls.com/stringbuilder
Munesh Sharma
11y
0
If we want to do some operations on strings like concat,trim,reverse,... we can use StringBuilder class.we are unable to do operations on String class
Mahesh Kumar alanka
11y
0
To make string mutable we need to use it is a class basically for similar kind
c# interview questions
refer this link
Mohit Kumar
11y
0
StringBuilders is mutable class. this string can be changed
deepak verma
11y
0
Tricky question.Some people has the knowledge when we get multiple string concat operations we chose StringBuilder over Strings. But we need to remember that StringBuilder internally uses array.What if the concat operation happens only twice?Which one is too chose.My experience tells me, I would like to chose StringBuilder over Strings when there is consequitive concat operations more than 6 times. Otherwise, I will chose simple Strings.
Arunava Bhattacharjee
11y
0
What is the difference between string.Empty and ""?Which one is best to use?
What is the difference between Lambda and Query Expression?
Message