-
-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: Append interpolated or concatenated string to Append calls #1562
Comments
Following code will use
Following code will be converted into previous case by RCS1267.
Following code could be added to RCS1267 as an improvement.
|
Yes, I know about the AppendInterpolatedStringHandler thing. var sb = new StringBuilder();
var var1 = DateTime.Now.ToString();
for (var i = 0; i = 100; i++) {
sb.Append(somethingElse(i));
}
sb.Append($"ABC{var1}EFG"); or sometimes, the Leaving the AppendInterpolatedStringHandler thing there, will generate a lot of intermediate string instances, cause extra memory copying, and put burden to the GC. If we break it into |
Not sure if I understand. Why it couldn't be substitued?
Why it matters if it's in a loop or not?
Where do you see these allocations in the SharpLab example? |
You are right. From .NET 6 on, there is no significant performance degrade when calling StringBuilder.Append(interpolated string). Sorry, I did not mentioned that the situation was that the code was compiled for the old .NET Framework where no AppendInterpolatedStringHandler exists. I checked the decompiled code from an assembly and the code is compiled as a call to String.Format, then StringBuilder.Append(string). |
Given the following code:
The third line used string interpolation, which is unnecessary, can be refactored to a series of
Append
calls as the following code shows.Moreover, it is also useful to change
Append
concatenated string toAppend
calls as well. For instance, the following can also be refactored to the above statement.The text was updated successfully, but these errors were encountered: