Just out of curiosity (not expecting a tangible result), which of the following codes is better for performance?
private void ReplaceChar(ref string replaceMe) {
if (replaceMe.Contains('a')) {
replaceMe=replaceMe.Replace('a', 'b');
}
}
private void ReplaceString(ref string replaceMe) {
if (replaceMe.Contains("a")) {
replaceMe=replaceMe.Replace("a", "b");
}
}
In the first example, I use char, and in the second, the lines in Contains () and Replace ()
Will the former have better performance due to less char memory, or will the latter work better because the compiler should not perform this operation?
(Or is it all nonsense because the CLR generates the same code in both cases?)
source
share