string s = "value_test_this";
string m = s.Replace('e','E');
StringBuilder strBuilder = new StringBuilder("value_test_this");
strBuilder.Replace('e','E');
since strings are immutable, how does the Replace works in string class,
string s = "value_test_this";
string m = s.Replace('e','E');
StringBuilder strBuilder = new StringBuilder("value_test_this");
strBuilder.Replace('e','E');
since strings are immutable, how does the Replace works in string class,
It creates another string in the memory and then points the m to that new string. The old string also stays in the memory.
And that is exactly why StringBuilder should be used if frequent modifications have to be made to the string.
If you want to know why Strings are immutable in C#, look at this SO discussion
s is definitely not pointed to the new string. The Replace method, along with many other string methods, creates a brand new string based on some logic, and returns this new string. s remains unchanged. The variable m is a reference to the new string. You need to replace s with m in your answer.