Click here to Skip to main content
15,868,164 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I get my if-statements to work for my bmi caculator? C# Pin
Thomas Daniels30-Dec-17 20:48
mentorThomas Daniels30-Dec-17 20:48 
Questionuser settings on a server C# Pin
Member 1342240130-Dec-17 9:43
Member 1342240130-Dec-17 9:43 
AnswerRe: user settings on a server C# Pin
Dave Kreskowiak30-Dec-17 12:21
mveDave Kreskowiak30-Dec-17 12:21 
AnswerRe: user settings on a server C# Pin
Gerry Schmitz1-Jan-18 7:37
mveGerry Schmitz1-Jan-18 7:37 
Questionobject location Pin
Member 1296407930-Dec-17 7:58
Member 1296407930-Dec-17 7:58 
AnswerRe: object location Pin
Dave Kreskowiak30-Dec-17 12:15
mveDave Kreskowiak30-Dec-17 12:15 
QuestionMessage Closed Pin
30-Dec-17 3:27
Jason132330-Dec-17 3:27 
AnswerRe: Difference between String and StringBuilder? Pin
OriginalGriff30-Dec-17 4:29
mveOriginalGriff30-Dec-17 4:29 
Strings are immutable: you cannot change a string once created. Any attempt to change it generates a new string.
So this:
C#
string a = "hello";
string b = a;
b += " world";
Means that a and b do not contain the same string: a has "hello" and b has "hello world". All string modification operations return a new string, the old is unmodified.

That's a problem if you are generating a big string in little pieces: adding lines to a chunk of text before you write it to a file for example:
C#
string myBook = "";
foreach(string line in myDatabaseRows)
   {
   myBook += line;
   }
This allocates a new string each time you add, so it starts to use up memory at a rapid rate of knots - and it starts to get quite slow, as each time you add a new line, you have to allocate memory for the new output, and copy the whole of the existing text into it, add the extra line, and move on. That's very inefficient!

That's where a StringBuilder comes in: It's a string which can be changed, or grown:
C#
StringBuilder myBook = new StringBuilder();
foreach(string line in myDatabaseRows)
   {
   myBook.Append(line);
   }
string output = myBook.ToString();
This does a lot fewer allocate-and-copy operations (it allocates an amount of space at the start and if you exceed that, it allocates twice as much and copies everything over) so it's a whole load more efficient - particularly if you can guess how much space you need and allocate that at the beginning:
C#
StringBuilder myBook = new StringBuilder(1000000);
Will give you a megabyte to start with, and if you don't exceed that it's the only allocation that is done - no "extra" copyiong is ever needed.

They are also needed if you start interop working with native code which doesn't know that strings are immutable - you hand them a StringBuilder and they can fill it in as needed. But that's the advanced stuff!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

AnswerRe: Difference between String and StringBuilder? Pin
Gerry Schmitz30-Dec-17 6:10
mveGerry Schmitz30-Dec-17 6:10 
QuestionHow to access a datagrid control (on a parent window) from a child window in wpf c# Pin
Hervend29-Dec-17 7:23
Hervend29-Dec-17 7:23 
AnswerRe: How to access a datagrid control (on a parent window) from a child window in wpf c# Pin
Dave Kreskowiak29-Dec-17 8:59
mveDave Kreskowiak29-Dec-17 8:59 
AnswerRe: How to access a datagrid control (on a parent window) from a child window in wpf c# Pin
Mycroft Holmes29-Dec-17 11:46
professionalMycroft Holmes29-Dec-17 11:46 
AnswerRe: How to access a datagrid control (on a parent window) from a child window in wpf c# Pin
Gerry Schmitz30-Dec-17 6:04
mveGerry Schmitz30-Dec-17 6:04 
GeneralRe: How to access a datagrid control (on a parent window) from a child window in wpf c# Pin
Hervend30-Dec-17 6:31
Hervend30-Dec-17 6:31 
QuestionWhat's the deal with Span<T> & Memory<T>? Pin
swampwiz28-Dec-17 22:06
swampwiz28-Dec-17 22:06 
AnswerRe: What's the deal with Span<T> & Memory<T>? Pin
Pete O'Hanlon28-Dec-17 23:40
subeditorPete O'Hanlon28-Dec-17 23:40 
QuestionC# Pin
Member 1359870428-Dec-17 20:16
Member 1359870428-Dec-17 20:16 
AnswerRe: C# Pin
Chris Quinn28-Dec-17 20:35
Chris Quinn28-Dec-17 20:35 
AnswerRe: C# Pin
OriginalGriff28-Dec-17 21:37
mveOriginalGriff28-Dec-17 21:37 
AnswerRe: C# Pin
Dave Kreskowiak29-Dec-17 2:33
mveDave Kreskowiak29-Dec-17 2:33 
GeneralRe: C# Pin
OriginalGriff29-Dec-17 4:03
mveOriginalGriff29-Dec-17 4:03 
GeneralRe: C# Pin
Dave Kreskowiak29-Dec-17 4:13
mveDave Kreskowiak29-Dec-17 4:13 
Questionappend message in sql server Pin
Member 1320094428-Dec-17 6:43
Member 1320094428-Dec-17 6:43 
AnswerRe: append message in sql server Pin
Dave Kreskowiak28-Dec-17 7:22
mveDave Kreskowiak28-Dec-17 7:22 
GeneralRe: append message in sql server Pin
Member 1320094431-Dec-17 18:16
Member 1320094431-Dec-17 18:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.