1

I know that string in C# is an immutable type. Is it true that when you chain string functions, every function instantiates a new string?

If it is true, what is the best practice to do too many manipulations on a string using chaining methods?

alisabzevari
  • 7,178
  • 6
  • 38
  • 60
  • 3
    [StringBuilder](https://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx) – Sayse Jun 22 '15 at 06:39
  • 1
    1) Usuallay, yes. 2) You can use `StringBuilder` to generate mutable string. – Soner Gönül Jun 22 '15 at 06:39
  • @Sayse: I thought every method of string that returns string instantiates a new string! – alisabzevari Jun 22 '15 at 06:41
  • Are you looking for [this](https://stackoverflow.com/questions/3069416/difference-between-string-and-stringbuilder-in-c-sharp) – Rohit Vipin Mathews Jun 22 '15 at 06:42
  • possible duplicate of [What's the best string concatenation method using C#?](http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c) – C0d1ngJammer Jun 22 '15 at 06:45
  • @manuchao OP is not necessarily talking about string concatenation. There are lots of other string manipulation methods. – poke Jun 22 '15 at 06:55

3 Answers3

3

Is it true that when you chain string functions, every function instantiates a new string?

In general, yes. Every function that returns a modified string does so by creating a new string object that contains the full new string which is stored separately from the original string.

There are certain ways to avoid this, especially using StringBuilder when you create the string from various parts. But in general, it’s difficult to say how you could make something better without creating multiple in-between string objects.

But as bad as this sounds, it usually isn’t a big problem: While it can be expensive, extraneous string objects because of string manipulations are very rarely a source of bad performance. Unless you are doing high-performance stuff with strings, you can probably ignore the fact that you keep creating new objects which are then thrown away. If you have doubts later, you can always profile your code to find out if it really matters or not.

poke
  • 307,619
  • 61
  • 472
  • 533
  • I found that many of `StringBuilder` methods return a `StringBuilder`. I don't know if they return a new `StringBuilder` or it is the same as before. So can we chain `StringBuilder` methods without worry to consume memory? – alisabzevari Jun 22 '15 at 06:49
  • 1
    `StringBuilder` methods always return the same object, so you can do `var b = new StringBuilder();` and then `b.Append("some string").Append("other string").Append(12).Append("foo bar");` and it will all append to the same object `b`. Once you are done with it, you can call `b.ToString()` to get the final string object. – poke Jun 22 '15 at 06:52
1

We used in our models which were bound to the UI a special method to get a unique id for that object. The method would concatenate some strings using the + operator.

It ended up being called 10000 times or more in a second sometimes which slowed down the entire application when loading new data. It was a serious bottleneck when first loading some screens in the application. So I guess if you are not calling the code which use string operations at least a couple of thousand times a second then you are okay.

There is also the string.Format method that is a couple of times faster in the long run.

Alecu
  • 2,295
  • 2
  • 24
  • 45
1

I know that string in C# is an immutable type. Is it true that when you chain string functions, every function instantiates a new string?

Yes, strings are immutable and thus they cannot be changed - at all. But not every function will instantiate a new string as some may be susceptible to string interning (strings of the same text can (but not always!) inhabit the same memory space)

If it is true, what is the best practice How to do too many manipulations on a string?

The most two common methods used are either to build a string up first using StringBuilder or via a String.Format

Sayse
  • 38,955
  • 14
  • 69
  • 129
  • 1
    Note that string interning **can** happen for the same string contents, but it may not always be the case. So you should not rely on it to magically fix your issues. – poke Jun 22 '15 at 06:54
  • @poke - Agreed, Just didn't want the OP to believe it is *always* a new string :) – Sayse Jun 22 '15 at 06:55