I have the following intentionally trivial function:
void ReplaceSome(ref string text) { StringBuilder sb = new StringBuilder(text); sb[5] = 'a'; text = sb.ToString(); }
It appears to be inefficient to convert this to a StringBuilder to index into and replace some of the characters only to copy it back to the ref’d param. Is it possible to index directly into the text param as an L-Value?
Or how else can I improve this?
C# strings are ‘immutable,’ which means that they can’t be modified. If you have a string, and you want a similar but different string, you must create a new string. Using a StringBuilder as you do above is probably as easy a method as any.